// First the 'send part':
// sndBuffer is declared as: std::string sndBuffer ( 32768, 0 )
// As you can see, it's quite a large buffer.
// Also, the string 'cmd' looks like "NAME <insertnamehere>"
sndBuffer = "ENTER ";
sndBuffer += cmd.substr( 5, cmd.length() - 5 );
send ( clientsocket, (char*)sndBuffer.c_str(), sndBuffer.length() * sizeof(char), 0 );
// Second, the 'recv' part (serverside):
// rcvBuffer is declared just like sndBuffer above.
// I used WSAAsyncSelect so it respons to FD_READ (hence the wParam)
recv ( wParam, (char *)rcvBuffer.c_str(), 32768, 0 );
Problems with send() or recv() - empty strings...
Hello, I have a *slightly* annoying problem that somehow send() sends more than what I'd like it to send, or recv() (on the server side) receives more than I'd like... I'll just go ahead and show the code so you see what I mean.. Once the data is received the HandleRecv() function gets called which handles the data. In this function I used MessageBox() to check the value of rcvBuffer. The strange thing is, it pops up about three times with no content before the actual rcvBuffer is displayed. This leads me to believe that somehow "send()" in the client sends more data than it should, or recv() in the server gets called multiple times... Any clue to what might be wrong? [Edited by - rogierpennink on June 30, 2006 8:23:17 AM]
It's pretty much impossible to tell from that code, but remember that one send() doesn't always equal one recv(), because TCP is stream based.
send() will not send more than it should (Otherwise you'd get all sort of security leaks), so the problem has to be that either your code is calling send() several times, or the recv() code isn't working correctly.
recv() will tell you the number of bytes it actually recieved, you could log that and tell how many bytes in total are recieved.
send() will not send more than it should (Otherwise you'd get all sort of security leaks), so the problem has to be that either your code is calling send() several times, or the recv() code isn't working correctly.
recv() will tell you the number of bytes it actually recieved, you could log that and tell how many bytes in total are recieved.
Looking over my original post again I have to agree it's not all that informative.
Basically, I'm practising networking with a client/server chat protocol. When the client presses 'connect' a "NAME <insertnamehere>" is sent to the server. If the name already exists, the server sends back a "NAME !INVALIDNAME", otherwise it sends the same thing back which is then interpreted as a "confirmation" by the client.
As soon as the name is confirmed, the client sends an "ENTER <insertnamehere>" to the server, which will then be broadcasted by the server to all connected clients ( using a simple for loop ). It is with the "ENTER" that things go wrong.
On the serverside I have a simple function for outputting text to the screen and before the "HandleRecv" function gets called I blindly display any received text on the screen which thus acts like a 'log'.
Here follows the code upon FD_READ in the server:
All that "UpdateOutput" does is add a timestamp to it and put it in the output EDIT control. The output is as follows:
As you can see, it receives an empty string two times...
I hope I have clarified it a bit...
Basically, I'm practising networking with a client/server chat protocol. When the client presses 'connect' a "NAME <insertnamehere>" is sent to the server. If the name already exists, the server sends back a "NAME !INVALIDNAME", otherwise it sends the same thing back which is then interpreted as a "confirmation" by the client.
As soon as the name is confirmed, the client sends an "ENTER <insertnamehere>" to the server, which will then be broadcasted by the server to all connected clients ( using a simple for loop ). It is with the "ENTER" that things go wrong.
On the serverside I have a simple function for outputting text to the screen and before the "HandleRecv" function gets called I blindly display any received text on the screen which thus acts like a 'log'.
Here follows the code upon FD_READ in the server:
if ( event == FD_READ ) { // Put the read stuff in the rcvBuffer after the matching client has been found for ( int i = 0; i < MAX_CLIENTS; i++ ) { // if we found a match, receive the message if ( client.clientsocket == wParam && client.connected != 0 ) { int iBytesReceived = recv ( client.clientsocket, (char *)rcvBuffer.c_str(), 32768, 0 ); // format the output std::stringstream * tmp = new std::stringstream; *tmp << "[" << inet_ntoa ( client.sockaddr.sin_addr ) << "]: " << rcvBuffer; UpdateOutput ( hOutput, tmp->str() ); delete tmp; HandleRecv ( i ); break; } }}
All that "UpdateOutput" does is add a timestamp to it and put it in the output EDIT control. The output is as follows:
[12:16:02] [127.0.0.1]: NAME Roger[12:16:03] [127.0.0.1]: ENTER Roger[12:16:03] [127.0.0.1]: [12:16:03] [127.0.0.1]:
As you can see, it receives an empty string two times...
I hope I have clarified it a bit...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement