Need Help Regarding Socket Programming in C#

  • 12 years ago

    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());

    }

    }

  • 12 years ago

    Increment a sequence number everytime u send the data, maintain this sequence number in a collection. After receiving the data, remove the corresponding sequence number from the collection.

    Finally, check whether collection contains any value, if so then some data have been missed

  • 12 years ago
    That end of data i am not able to recognize. Interestingly my server doen't send anything which tells end of data. So i am trying to find out the way through which i can decide the end of data. Can you let me know the way to find out this.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Brevity is the soul of wit” - Shakespeare