I have a client in Java and a server in C++ (using wsock32.lib). The client is very simple, just sends a text to server. I have the server loop to receive a message from client, like this:
CODE
while(1){
int nret = 0;
nret = recv(m_socket,buffer,100,0);
if(nret == SOCKET_ERROR) {
nret = WSAGetLastError();
//throw error
}
buffer[nret-1] = '\0';
printf("Recv:%s\n",buffer);
}
When the client sends a message, ended by "\n", e.g. "hello\n", I debug, and the process is hereafter:
- server calls recv for the first time. buffer gets "h????????????", nret = 1. so the buffer is got "" after truncating.
- it continues to call recv, buffer gets "ello????????", nret = 5. the buffer is "ello" then.
I don't know why the recv called 2 times. Please help me! Thank you in advance.
Best regards,
Voicon