Hi Everyone,
I am working on Socket Programming...I am using Socket Class for connection...And i am sending commands and able to receive the response asynchronously...I am receving the response in small small data packets....
SO can anyone tell me how to recognize that i receved last data packet...I would like to know whether i receved comlpete data or not????
YOu can find the code what i am using below....
For Sending commands.......
NetworkStream networkStream = new NetworkStream(m_clientSocket);System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream);
streamWriter.WriteLine(msg);
streamWriter.Flush();
For Receving any response i am using the following code
private void WaitForData()
{
try
{
if (m_pfnCallBack == null)
{
m_pfnCallBack = new AsyncCallback(OnDataReceived);
}
SocketPacket theSocPkt = new SocketPacket();
theSocPkt.thisSocket = m_clientSocket;
// Start listening to the data asynchronously
m_result = m_clientSocket.BeginReceive(theSocPkt.dataBuffer,
0, theSocPkt.dataBuffer.Length,
SocketFlags.None,
m_pfnCallBack,
theSocPkt);
}
catch (SocketException se)
{
// Console.WriteLine(se.Message.ToString());
}
}
private void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
int iRx = theSockId.thisSocket.EndReceive(asyn);char[] chars = new char[iRx];
System.Text.
Decoder d = System.Text.Encoding.UTF8.GetDecoder();int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.
String szData = new System.String(chars);Thread.Sleep(400);
Response += szData;
//richTextRxMessage.Text = richTextRxMessage.Text + szData;
//Console.WriteLine(szData);
WaitForData();
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
}
catch (SocketException se)
{
// Console.WriteLine(se.Message.ToString());
}
}
Enter your message below
Sign in or Join us (it's free).