CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Join Date
    Jun 2009
    Posts
    87

    [RESOLVED] ASyncSocket Receive Problem

    Hi to All,

    In my project i am using CASyncSocket to communicate with server. Some times server send information like "A2BC#Message:aaa", "TEXT#AMessage:bbbbb",but at client side bufferlenght=4096.
    AsyncSocket::OnReceive(error)
    {
    int a=mysocket.Receive(buffer,bufferlength);
    }

    it first receives only one charecter that is 'A', then after some times it receives remaining data.
    some times after receiving one char,then omit the "2BC#Message:aaa" start receiving second message first char 'T', then it might be receive second message remaining data.
    Received data ==AT.

    Is there any problem in my program or AsyncSocket?
    Please help me
    it causes more problem with slow network connections.

    Thanks in Advance,
    Bharath

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: ASyncSocket Receive Problem

    Is there any problem in my program or AsyncSocket?
    No. Although you send the information in 1 send, the network itself decides how the data is send/received. You can never count on the fact that the data is received all at once. What you need to do is collect the data. So, create a buffer that is filled in the OnReceive, and keep adding the data that you receive. Every time you received something you check how much data is in that buffer, and if there is enough in it you can read and parse it.

  3. #3
    Join Date
    Jun 2009
    Posts
    87

    Re: ASyncSocket Receive Problem

    i did the like that only, received data stored on some variable and appended the second receive data to variable its fine.
    My problem is If server sends "A2BC#Message:abcd" and "TEXT#Message:abcdefgh"
    first receive data is A only and the appended data is T. what about "2BC#Message:abcd".
    i think tcp buffer may store all data in queue. what about this missing data? It happening in slow network connections. how can i come out from this problem. First of all how can i get Buffer lenght
    or tcp buffer information is there any data available or not.



    thanks,
    Bharath

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: ASyncSocket Receive Problem

    1. Do you check the return value of this call:
    Code:
    int a=mysocket.Receive(buffer,bufferlength);
    2. See the example in MSDN article CAsyncSocket::OnReceive
    Victor Nijegorodov

  5. #5
    Join Date
    Jun 2009
    Posts
    87

    Re: ASyncSocket Receive Problem

    the return value of Receive() was 1 when it received 1 char.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: ASyncSocket Receive Problem

    Quote Originally Posted by bharathkumarreddy.s View Post
    Hi to All,

    In my project i am using CASyncSocket to communicate with server. Some times server send information like "A2BC#Message:aaa", "TEXT#AMessage:bbbbb",but at client side bufferlenght=4096.
    AsyncSocket::OnReceive(error)
    {
    int a=mysocket.Receive(buffer,bufferlength);
    }
    Quote Originally Posted by bharathkumarreddy.s View Post
    the return value of Receive() was 1 when it received 1 char.
    Are you sure that sender did send all 4096 bytes, not only the first one ('A' or 'T')?
    Did it send a Unicode or ANSI sequence ?
    Victor Nijegorodov

  7. #7
    Join Date
    Jun 2009
    Posts
    87

    Re: ASyncSocket Receive Problem

    Actually my server is written in java. That server sends in the form of bytes, not sure 4096, but i know every time it sends one message not sigle character. At client side receiving buffer size is 4096.

    One more thing, I saw some of posts regarding AsyncSocket, most of the people wrote OnReceive() method is not calling, may be chance same as in my case.

    or any system busy or network busy may cause this....



    Thanks,
    Bharath

  8. #8
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: ASyncSocket Receive Problem

    or any system busy or network busy may cause this....
    Highly unlikely. TCP is a very stable protocol, even with slow busy networks. Don't think this is a form of packet/data loss. I think you are simply missing something. You said the server is written in Java, in that case, try to answer the question of VictorN. Did it send a Unicode or ANSI sequence ?

  9. #9
    Join Date
    Jun 2009
    Posts
    87

    Re: ASyncSocket Receive Problem

    Quote Originally Posted by Skizmo View Post
    Highly unlikely. TCP is a very stable protocol, even with slow busy networks. Don't think this is a form of packet/data loss. I think you are simply missing something. You said the server is written in Java, in that case, try to answer the question of VictorN. Did it send a Unicode or ANSI sequence ?
    By default java takes Unicode.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: ASyncSocket Receive Problem

    Could it happen that sender was trying to send this Unicode text not as a byte array but as a char (ANSI) array? In such a case the second byte of UNICODE 'A' would be treated as the end of string and no more data would be sent.
    Victor Nijegorodov

  11. #11
    Join Date
    Jun 2009
    Posts
    87

    Re: ASyncSocket Receive Problem

    But some times it receives entire message at a time,some times taking few seconds and receive remaining data.

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: ASyncSocket Receive Problem

    Quote Originally Posted by bharathkumarreddy.s View Post
    But some times it receives entire message at a time,some times taking few seconds and receive remaining data.
    WEll, it is just a standard behaviour! It is by design of TCP/IP (and Skizmo already pointed it out!)


    Quote Originally Posted by bharathkumarreddy.s View Post
    ... My problem is If server sends "A2BC#Message:abcd" and "TEXT#Message:abcdefgh"
    first receive data is A only and the appended data is T. what about "2BC#Message:abcd".
    i think tcp buffer may store all data in queue. what about this missing data? ...
    And this behavior seems very strange. I cannot be possible unless either sender or receiver does its job wrong (i.e code to send or to receive is wrong)
    Victor Nijegorodov

  13. #13
    Join Date
    Jun 2009
    Posts
    87

    Re: ASyncSocket Receive Problem

    How can we say receiver problem? Using OnReceive() and socket.Receive() methods to receive the data and receive Buffer size is 4096, if coming data is greater than 4096 maintaining in loop. if coming data splits same loop will manage. But here(in this case) some thing happens i need to find out with the help of experts like you people. I will try to send up to date status. If you found any clues please send me.



    Thanks,
    Bharath.

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: ASyncSocket Receive Problem

    It's hard to say you more without seeing your actual code (for both sender and receiver)
    Victor Nijegorodov

  15. #15
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: ASyncSocket Receive Problem

    Quote Originally Posted by bharathkumarreddy.s View Post
    ...Using OnReceive() and socket.Receive() methods to receive the data and receive Buffer size is 4096, if coming data is greater than 4096 maintaining in loop. if coming data splits same loop will manage. ....
    You should never call Receive() more than once in a single OnReceive() handler. It's incorrect to call Receive() in a loop. Call Receive() exactly once. According to the Winsock and CAsyncSocket framework, if more data is available in the buffer after the call to Receive(), the framework will generate another call to OnReceive at some indeterminate time in the future. The remainder of the buffer can be obtained in the second call to OnReceive (or at least some more of the buffer can be drained).

    Mike

    PS: And VictorN is correct: it's hard to diagnose further without seeing your code for both sender and receiver.

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured