0

I am trying to develop a simple sender/listener TCP/IP socket in C#; i.e. once connected, the sender application reads a character string typed in a textbox and sends it to a listener Windows Application throgh a syncronous TCP/IP socket. The listener code is here

receivingExit = true;
btnStopReceiving.Enabled  = true;
btnStartReceiving.Enabled = false;
while (receivingExit) {
   byte[] bytes = new byte[1024];
   MessageBox.Show("Waiting !!!"); //Line to be commented
   int bytesRec = workingSocket.Receive(bytes);
   MessageBox.Show("Received "+bytesRec+" bytes"); //Line to be commented
   String data =Encoding.ASCII.GetString(bytes,0,bytesRec);
   MessageBox.Show(data); //Line to be commented
   txtDiplay.Text = txtDiplay.Text+data+"\r\n";
}

If I leave the comments, everything works fine; i.e. in txtDisplay text box I can see the characters string I typed and sent by my sender application.... obviously I see the MessageBox(es) displaying; but once inserted the comments as above, it seems the listener Windows App blocks after get the connection with the sender and anything appears in txtDisplay... the same issue if I use a syncronous UDP/IP socket. With console application instaed, everything works fine. What the problems, and how could solve them ? Advanced thanks. Davide

  • without a good [mcve] that reliably reproduces the problem, it's impossible to say for sure what the problem is. However, most likely the issue is that the code you posted is running in the UI thread, preventing UI updates from being shown when you comment out the lines. When you use `MessageBox`, it runs an event-pumping loop and allows the update messages to get to the window, which in turn allows the text box to be redrawn. You should learn about asynchronous I/O and use that to deal with the network connection, and leave the UI thread to handle UI. – Peter Duniho Nov 09 '17 at 02:11
  • @Peter... I guess you're right, infact I start listening once pressed a 'start listening button', using the code I posted in its event handler; once connected. Anyway is there a way to use syncronous TCP(UDP) sockets in UI application ? Thanks for answering me. Davide – Davide Vacca Nov 09 '17 at 09:16
  • _"is there a way to use syncronous TCP(UDP) sockets in UI application"_ -- yes, but not any way worth learning. The two main mechanisms you might use are to 1) put the socket I/O in a dedicated thread, other than the UI thread, or 2) use polling on the socket in the UI thread. The latter is a particularly bad choice; the dedicated thread is fine for low-bandwidth, low-connection-count, but since you have to learn _something_ new regardless, you might as well just focus on asynchronous. FWIW, you might find [an answer I wrote](https://stackoverflow.com/a/44942011) earlier this year helpful. – Peter Duniho Nov 09 '17 at 09:23
  • @Peter, well the bandwith is very low, just few bytes/seconds; at least for my needs. Anyway I will try to asynchronous socket, just in order to have something else to learn. Very helpful, thank you very much – Davide Vacca Nov 09 '17 at 13:22

0 Answers0