/// <summary> /// Call back calling upon recieving the data from the client /// </summary> /// <param name="asyn"></param> private void OnDataReceived(IAsyncResult asyn) { try { CommObject theSocketId = (CommObject)asyn.AsyncState; int iRx = theSocketId.m_WorkScoket.EndReceive(asyn); char[] chars = new char[iRx + 1]; System.Text.Decoder d = Encoding.UTF8.GetDecoder(); int charLength = d.GetChars(theSocketId.buffer, 0, iRx, chars, 0); string szData = new string(chars); SetRcvText(szData); // rTbrcvmsg.Text = rTbrcvmsg.Text + szData; WaitForData(); } catch (ObjectDisposedException) { System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n"); } catch (SocketException se) { MessageBox.Show(se.Message); } }
/// <summary> /// Waiting for the data after connection /// </summary> private void WaitForData() { try { if (m_pfnCallBack == null) { m_pfnCallBack = new AsyncCallback(OnDataReceived); } CommObject thesocketpkt = new CommObject(); thesocketpkt.m_WorkScoket = m_clientSocket; // start listening to data asynchronously m_result = m_clientSocket.BeginReceive(thesocketpkt.buffer, 0, thesocketpkt.buffer.Length, SocketFlags.None, m_pfnCallBack, thesocketpkt); } catch (SocketException se) { MessageBox.Show(se.Message); } }