Problems with data received thru socket

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jesper Stocholm

    #1

    Problems with data received thru socket

    I am using the example [1] as the basis for an application I am
    currently writing. The setup consists of two applications (client and
    server) that talk to each other thru TCP.

    I have no problems with sending information back and forth, but I have
    problems _containing_ the data sent from the client to the server.

    In the class I instantiate/declare a StringBuilder to collect the
    data as it comes thru the socket

    private StringBuilder _sb = new StringBuilder() ;

    For each byte I read I append this byte to _sb, but somehow only
    the initial byte is appended to _sb - all other bytes are somehow
    lost. I also add all bytes to a listBox, so I have verified that the
    data from the client is actually received by my application.

    I have tried to display the data arriving (in this case the string
    "test") with

    System.String szData = new System.String(c hars);
    MessageBox.Show ("szData:" + szData + "\n" + "somestring ");

    The result is this:

    szData:t
    szData:e
    szData:s
    szData:t

    Whereas I would expext:

    szData:t
    somestring
    szData:e
    somestring
    szData:s
    somestring
    szData:t
    somestring

    So somehow the data is "disapearin g" for me.

    Can you tell me why?


    The relevant code on the server is this:

    public void OnDataReceived( IAsyncResult asyn)
    {
    try
    {
    CSocketPacket theSockId = (CSocketPacket) asyn.AsyncState ;
    //end receive...
    int iRx = 0 ;
    iRx = theSockId.thisS ocket.EndReceiv e (asyn);
    char[] chars = new char[iRx + 1];
    System.Text.Dec oder d = System.Text.Enc oding.UTF8.GetD ecoder();
    int charLen = d.GetChars(theS ockId.dataBuffe r,0, iRx, chars, 0);
    System.String szData = new System.String(c hars);
    listBox1.Items. Add(DateTime.No w.ToString() + "\t" + szData);
    _sb.Append(szDa ta); // this line is causing problems
    WaitForData(m_s ocWorker );
    }
    catch (ObjectDisposed Exception )
    {
    System.Diagnost ics.Debugger.Lo g(0,"1","\nOnDa taReceived: Socket has been closed\n");
    }
    catch(SocketExc eption se)
    {
    MessageBox.Show (se.Message );
    }
    }

    public void WaitForData(Sys tem.Net.Sockets .Socket soc)
    {
    try
    {
    if ( pfnWorkerCallBa ck == null )
    {
    pfnWorkerCallBa ck = new AsyncCallback (OnDataReceived );
    }
    CSocketPacket theSocPkt = new CSocketPacket ();
    theSocPkt.thisS ocket = soc;
    // now start to listen for any data...
    soc .BeginReceive (theSocPkt.data Buffer ,0,theSocPkt.da taBuffer.Length ,SocketFlags.No ne,pfnWorkerCal lBack,theSocPkt );
    }
    catch(SocketExc eption se)
    {
    MessageBox.Show (se.Message );
    }
    }


    thanks,


    [1] http://www.developerfusion.com/show/3997

    --
    Jesper Stocholm
    Copenhagen, Denmark
Working...