Problem sending an int...
Hello,
I''m writing a simple chat client/server that everyone writes when they learn socket programming.
The client is a win32 app, and the server is just a regular console app. For every message that the client sends to the server, it first sends a packet with the length. The length is just an integer. Kind of like this.
void SendMessageToServer(char *message, int length)
{
// Send length
send(clientsocket, (char *)&length, sizeof(int), 0);
// Send message
....
}
And then the server receives this length before the message, kind of like this.
void ReceiveMessage()
{
// Receive the length
recv(clients.clientsocket, (char *)&length, sizeof(int), 0);
// Receive the message
...
}
I then printf the value of length that the server receives, and no matter what, it is always 98. I check on the client side right before the send() and it is always the right length, usually around 5 or 6 characters, depending on how much I type. My guess is I''m trying to recv() this all wrong. Does anyone know what I could be doing wrong?
Thanks,
Eric
A quick question:
What does recv() return for you? My guess is -1 (which indicates an error). If it returns -1, then call WSAGetLastError() to figure out why it failed.
Keep in mind that when you send() a string, one call to recv() on the other side of the socket doesn't necessarily read the whole string at once in a single call. Most likely on a LAN with short text strings this will work a vast majority of the time.
[edited by - fingh on June 26, 2003 4:46:06 PM]
What does recv() return for you? My guess is -1 (which indicates an error). If it returns -1, then call WSAGetLastError() to figure out why it failed.
Keep in mind that when you send() a string, one call to recv() on the other side of the socket doesn't necessarily read the whole string at once in a single call. Most likely on a LAN with short text strings this will work a vast majority of the time.
[edited by - fingh on June 26, 2003 4:46:06 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement