Sign in to follow this  
LorenSoth

recv problem !

Recommended Posts

the client program sends 20 bytes of data
char sendPacket[20] ;
int nofpacket = 20 ;
send (Socket,&sendPacket[0],nofpacket,0);
and the server receives by this code
unsigned short receivedpacketnumber ;

recv (Socket,&receivedpacketnumber,2,0);
recv (Socket,&recvPacket[0],receivedpacketnumber-2,0);
this seems okay as you see the first 2 bytes is the packet size, and then i read the rest by this amount. Everything seems ok but the problem occurs if the recv function was in waiting mode ! In server, if the packet is arrived before recv and then recv is invoked then there is no problem but if the recv gets no data and get into waiting state and then data come, it just gets the wrong data. It just gets in this way : (numbers are not real) send (Socket,&sendPacket[0],nofpacket,0); Send Data : 20 21 26 54 84 51 35 65 84 15 25 28 50 54 35 21 15 54 84 87 recv (Socket,&receivedpacketnumber,2,0); Received 2 Byte : 26 54 instead of 20 21 hope you understand me. Thanks.

Share this post


Link to post
Share on other sites
send() and recv() are very well debugged. Chances are, there's something in your application logic that causes this problem.

Note that recv() may not return as many bytes as you ask for. If your protocol depends on reading the full amount (which most protocols do), you will need to loop back until you've gotten all the data, else the next recv() will be out of sync.

Looking at your data, it looks like there is an additional recv() for 2 bytes executed somewhere in your application, as what you get next is bytes 2 and 3 of what you sent.

Share this post


Link to post
Share on other sites
I will assume you're using TCP over IP and this is a standard sockets implementation.

Both your recv() calls assume you'll get precisely the amount of data you're asking for. recv(10 bytes) will very likely occasionally return 8 bytes, 5 bytes, or even just 1 byte. You need to stick them in loops and watch the return value to see how much you're getting back.

Something like:


for(receivedSoFar = 0; receivedSoFar < amountExpected; ) {
received = recv(socket, dataPtr+receivedSoFar,receivedSoFar - amountExpected);
if( received <= 0 ) {
//error handling
}
receivedSoFar += received;
}


Same with send.

Share this post


Link to post
Share on other sites
[quote]
If your protocol depends on reading the full amount (which most protocols do), you will need to loop back until you've gotten all the data, else the next recv() will be out of sync.
[\quote]

I think thats it. Server just sends 20 bytes and then i use 2 recv to get that data but i did like that because it wont be 20 bytes all the time, it is not constant.When i do it like that the synchronization fails.

So it seems reading unless there is no data is the best way. Thank you again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this