-
April 26th, 2010, 01:39 AM
#1
[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
-
April 26th, 2010, 01:45 AM
#2
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.
-
April 26th, 2010, 02:12 AM
#3
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
-
April 26th, 2010, 03:49 AM
#4
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
-
April 26th, 2010, 05:25 AM
#5
Re: ASyncSocket Receive Problem
the return value of Receive() was 1 when it received 1 char.
-
April 26th, 2010, 05:31 AM
#6
Re: ASyncSocket Receive Problem
Originally Posted by bharathkumarreddy.s
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);
}
Originally Posted by bharathkumarreddy.s
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
-
April 26th, 2010, 11:31 PM
#7
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
-
April 27th, 2010, 01:04 AM
#8
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 ?
-
April 27th, 2010, 04:02 AM
#9
Re: ASyncSocket Receive Problem
Originally Posted by Skizmo
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.
-
April 27th, 2010, 04:10 AM
#10
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
-
April 27th, 2010, 06:37 AM
#11
Re: ASyncSocket Receive Problem
But some times it receives entire message at a time,some times taking few seconds and receive remaining data.
-
April 27th, 2010, 06:44 AM
#12
Re: ASyncSocket Receive Problem
Originally Posted by bharathkumarreddy.s
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!)
Originally Posted by bharathkumarreddy.s
... 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
-
April 28th, 2010, 12:15 AM
#13
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.
-
April 28th, 2010, 01:11 AM
#14
Re: ASyncSocket Receive Problem
It's hard to say you more without seeing your actual code (for both sender and receiver)
Victor Nijegorodov
-
April 28th, 2010, 12:48 PM
#15
Re: ASyncSocket Receive Problem
Originally Posted by bharathkumarreddy.s
...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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|