3
Answers

Socket Eventhandler on Receive?

Ask a question
Dan Ho

Dan Ho

13y
5.1k
1
So I downloaded an example program that shows socket communication here: http://www.developerfusion.com/article/3918/socket-programming-in-c-part-1/

You enter the IP + Port and connect to the server (or in my case its a device that can be plugged to the network).

Basically you can enter a text to the first textbox and when you click Tx it will send it to the server.
If you press Rx it will receive the response and paste it as string into the second textbox, heres a snippet of the receive function:

  private void cmdReceiveData_Click(object sender, System.EventArgs e)
  {
  try
  {
  byte [] buffer = new byte[1024];
  int iRx = m_socWorker.Receive (buffer);
  char[] chars = new char[iRx];

System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
  int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
  System.String szData = new System.String(chars);
  txtDataRx.Text = szData;
  }
  catch(System.Net.Sockets.SocketException se)
  {
  MessageBox.Show (se.Message );
  }
  }


Now I'd like to have a function/Eventhandler that automatically detects whenever there's a response and automatically enters it to the second textbox without having to press Rx, just like the System.IO.Ports.SerialDataReceivedEventArgs handler.
Is there such a thing for sockets and is it possible to implement here?
If you try to answer or help me please not that i'm a rookie when it comes down to programming.

Thanks

Answers (3)
Next Recommended Forum