I write a code using socket to realize multiple message exchanges between a client and a server (i.e., a protocol running between A and B). But when I use recv() on a client to receive a message from a server, it turned out that the returned receive size is not what I ask for, and of course the whole program is wrong. Why does this happen? How to solve it?
Below is my code snippet.
...... bytesRecv = recv(recv_socket, recv_buff, 1000, 0); if(bytesRecv == SOCKET_ERROR) printf(" RECVBIG recv() error %ld.\n", WSAGetLastError()); else printf(" RECVBIG recv() OK - Bytes received: %ld\n", bytesRecv); ......
I use the above code, but it shows that:
RECVBIG recv() OK - Bytes received: 260.
,and in the next call to recv, it shows that:
RECVBIG recv() OK - Bytes received: 740.
Not all calls to recv() are wrong, some of them work well. Also, client and server use the same recv() code snippet, but all recv() calls at the server's side are correct.
What's the problem？