-
[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
-
Re: ASyncSocket Receive Problem
Quote:
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.
-
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
-
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
-
Re: ASyncSocket Receive Problem
the return value of Receive() was 1 when it received 1 char.
-
Re: ASyncSocket Receive Problem
Quote:
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);
}
Quote:
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 ?
-
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
-
Re: ASyncSocket Receive Problem
Quote:
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 ?
-
Re: ASyncSocket Receive Problem
Quote:
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.
-
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. :confused:
-
Re: ASyncSocket Receive Problem
But some times it receives entire message at a time,some times taking few seconds and receive remaining data.
-
Re: ASyncSocket Receive Problem
Quote:
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!)
Quote:
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)
-
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.
-
Re: ASyncSocket Receive Problem
It's hard to say you more without seeing your actual code (for both sender and receiver)
-
Re: ASyncSocket Receive Problem
Quote:
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.
-
Re: ASyncSocket Receive Problem
Thank you Mr Mike,
I dint called more than once Receive() in OnReceive().
One small information who is calling the OnReceive()?
1.System commands
2.Socket event.
What is the behind story of AsyncSocket,OnReceive() and Receive() methods?
Thanks,
Bharath.
-
Re: ASyncSocket Receive Problem
-
Re: ASyncSocket Receive Problem
Quote:
Originally Posted by
VictorN
I studied that articals. Even though i didn't find my problem.
-
Re: ASyncSocket Receive Problem
Have you verified server operation by using a protocol analyser like WireShark?
-
Re: ASyncSocket Receive Problem
Quote:
Originally Posted by
bharathkumarreddy.s
... I dint called more than once Receive() in OnReceive().
Then why is it that you stated that you "maintain[ed] in loop" "if coming data is greater than 4096"?
Never mind, please don't bother to answer.
Please show your code for send and receive, as mentioned by VictorN above.
Quote:
One small information who is calling the OnReceive()?
1.System commands
2.Socket event.
What is the behind story of AsyncSocket,OnReceive() and Receive() methods?
The details are complex, but the basic idea is this: When there is a new socket event (such as the arrival of new data in the incoming buffer), Windows converts the event into an ordinary Windows message which it posts to the application's message queue. In the MFC message loop, when it encounters one of these messages, the MFC framework converts it into a call to the appropriate virtual function, such as a call to your implementation of the virtual OnReceive() function.
These details are explained in the links that VictorN gave you.
Mike
-
Re: ASyncSocket Receive Problem
Quote:
Originally Posted by
MikeAThon
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.
Thank you.
-
Re: ASyncSocket Receive Problem
Quote:
Originally Posted by
jeron
Have you verified server operation by using a protocol analyser like WireShark?
Thank you jeron,
I solved my problem. here the problem is java socket outputstream writeBytes();
I just changed this method to write(data.getByteArray());
Thank you for all of you,
special thanks to VictorN.
-
Re: ASyncSocket Receive Problem
Quote:
Originally Posted by
VictorN
It's hard to say you more without seeing your actual code (for both sender and receiver)
I solved my problem, problem with java socket outputstream WriteBytes().
I solved that with replacing that method with write(data.getByteArray())
Thank you very much mr.VictorN.
-
Re: ASyncSocket Receive Problem
-
Re: ASyncSocket Receive Problem
VictorN please help me how to find Application crash?
I wrote another thread regarding that problem,i din't find and reply from more than 5 days.
pls, reply me.
Thanks,
Bharath