[sockets]sending string problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: [sockets]sending string problem

  1. #1
    Join Date
    Nov 2005
    Posts
    102

    [sockets]sending string problem

    Hey, i have an Async nonblocking client/server application which should send strings to each other. Only the problem is that the string comes in in parts.
    So i send the following string with the client to the server:

    Hello

    Then the server recieves it like this:
    H
    e
    l
    l
    o


    But it is important that i can send a complete strings to the server. Anyone any idea why this isn't happening now??


    Client, send string button
    Code:
                try
                {
                    Object objData = "Hello";
                    byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString());
                    m_socClient.Send(byData);
                }
                catch (SocketException se)
                {
                    MessageBox.Show(se.Message);
                }
    Server: Receive string and display in messagebox
    Code:
            public void WaitForData(System.Net.Sockets.Socket soc)
            {
                try
                {
                    if (pfnWorkerCallBack == null)
                    {
                        pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
                    }
                    CSocketPacket theSocPkt = new CSocketPacket();
                    theSocPkt.thisSocket = soc;
                    // now start to listen for any data...
                    soc.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack, theSocPkt);
                }
                catch (SocketException se)
                {
                    MessageBox.Show(se.Message);
                }
    
            }
    
            public void OnDataReceived(IAsyncResult asyn)
            {
                try
                {
                    CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState;
                    //end receive...
                    int iRx = 0;
                    iRx = theSockId.thisSocket.EndReceive(asyn);
                    char[] chars = new char[iRx + 1];
                    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);
    
                    //Displays the string one letter at a time...
                    MessageBox.Show(szData);
                    WaitForData(m_socWorker);
                }
                catch (ObjectDisposedException)
                {
                    System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
                }
                catch (SocketException se)
                {
                    MessageBox.Show(se.Message);
                }
            }

  2. #2
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: [sockets]sending string problem

    How large is the byte buffer that your placing the received string into? If its only one byte, you will receive only one byte off of the internal buffer at a time.

    EDIT:

    Also, I cant help but notice your sending ASCII bytes, but receiving and decoding them into UTF-8. It should work regardless but just for consistency sake have you thought about receiving them and decoding them as ASCII?
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    Re: [sockets]sending string problem

    But it is important that i can send a complete strings to the server. Anyone any idea why this isn't happening now??
    Generally, this is impossible due to how TCP operates. If you send 100 bytes, they can arrive in a single 100 byte chunk or one hundred 1 byte chunks. Your application has to cope with that.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center