By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

Socket: C++ Server reads text wrongly from Java client

P: 2
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     while(1){
  3.                 int nret = 0;    
  4.             nret = recv(m_socket,buffer,100,0);
  5.             if(nret == SOCKET_ERROR) {
  6.                nret = WSAGetLastError();        
  7.                //throw error
  8.             }
  9.             buffer[nret-1] = '\0';
  10.         printf("Recv:%s\n",buffer);
  11.     }
  12.  
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
Mar 23 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.