How to call a function between two WinForm applications over a network?

I have a video kiosk setting in my lobby, it allows people to register and print an icon with their picture, name, etc. There is also a remote support tool, which, unfortunately, sometimes crashes. I have a function in the kiosk that fixes this problem, but you have to go to the kiosk to call it right away.

I also wrote a management tool that uses WMI to monitor and manage some other aspects of the kiosk. I would like to be able to run this recovery function through this application. I spent countless hours on Google trying to figure it out with no luck. Perhaps I was not looking for the right things.

My question is that. In C #, how can I call the restore function in a kiosk application from an admin application through a network?

+3
source share
1 answer

OK, in my form Server, I have a BackgroundWorker that runs a TcpListener . You will want to put this TcpListener in BackgroundWorker, otherwise you can never stop it from executing until it accepts TcpClient.

In addition, you need to process any data received from this background thread in the main execution thread to eliminate cross-thread exceptions:

private TcpListener _listener;
private const int port = 8000;

private void Worker_TcpListener(object sender, DoWorkEventArgs e) {
  BackgroundWorker worker = sender as BackgroundWorker;
  do {
    try {
      _listener = new TcpListener(IPAddress.Any, port);
      _listener.Start();
      TcpClient client = _listener.AcceptTcpClient(); // waits until data is avaiable
      int MAX = client.ReceiveBufferSize;
      NetworkStream stream = client.GetStream();
      Byte[] buffer = new Byte[MAX];
      int len = stream.Read(buffer, 0, MAX);
      if (0 < len) {
        string data = Encoding.UTF8.GetString(buffer);
        worker.ReportProgress(len, data.Substring(0, len));
      }
      stream.Close();
      client.Close();
    } catch (SocketException) {
      // See MSDN: Windows Sockets V2 API Error Code Doc for details of error code
    } catch (ThreadAbortException) { // If I have to call Abort on this thread
      return;
    } finally {
      _listener.Stop();
    }
  } while (!worker.CancellationPending);
}

This is not suitable for large messages (for example, JPEG files, etc.), but is great for short lines, where I encoded in special data to search.

( ReportProcess), :

private void Worker_TcpListener(object sender, ProgressChangedEventArgs e) {
  if (e.UserState != null) {
    int len = e.ProgressPercentage;
    string data = e.UserState.ToString();
    if (!String.IsNullOrEmpty(data) && (3 < len)) {
      string head = data.Substring(0, 3);
      string item = data.Substring(3);
      if (!String.IsNullOrEmpty(item)) {
        if (head == "BP:") {
          string[] split = data.Split(';');
          if (2 < split.Length) {
            string box = split[0].Substring(3); // Box Number
            string qty = split[1].Substring(2); // Quantity
            string customer = split[2].Substring(2); // Customer Name
            MyRoutine(box, qty, customer);
          }
        }
      }
    }
  }
}

.

10 Pocket PC , . VB, , , # , :

Private Sub SendToServer(string serialNum, int qty, string customer)
  Cursor.Current = Cursors.WaitCursor
  Try
    Dim strPacket As String = String.Format("BP:{0};Q:{1};C:{2};", serialNum, qty, customer)
    Dim colon As Integer = p7_txtIPAddress.Text.IndexOf(":")
    Dim host As String = p7_txtIPAddress.Text.Substring(0, colon)
    Dim port As Integer = CInt(p7_txtIPAddress.Text.Substring(colon + 1))
    Dim dataPacket As [Byte]() = Encoding.ASCII.GetBytes(strPacket)
    Using client As New TcpClient(host, port)
      Dim stream As NetworkStream = client.GetStream()
      stream.Write(dataPacket, 0, dataPacket.Length)
    End Using
  Catch err As Exception
    MessageBox.Show(err.Message, "Print To Server TCP Error")
  Finally
    Cursor.Current = Cursors.Default
  End Try
End Function

, , , .

, , , (, , ..), . , , .

, , .

+2

All Articles