Send() & Recv() : different size?

Started by
8 comments, last by Pegasus 19 years, 10 months ago
I''m really confused about something. I send on a tcp socket a message like "hello" the send function return 5 as the string has a length of 5. But the Recv function returns 6. I''ve tested with different messages, the revc always returns the actual size + 1. where can this come from? Thx
Advertisement
you can't receive data that you didn't send...
maybe you are also sending the null terminating byte of the message string?

[edited by - zppz on June 1, 2004 12:38:46 PM]
Post the code that assembles the string into char or byte before calling recv(), WSARecv(), etc.

Kuphryn
send code:

result=socket1.sendPacket("12345");

sendPacket function:
int sock::sendPacket(char* buffer)
{
int addr_len;
addr_len = sizeof(struct sockaddr);

packetOut.length=send(lesocket,buffer,strlen(buffer),0);
return packetOut.length;

}

recv code:

result=socket1.getPacket();

getPacket function:

int sock::getPacket()
{
int addr_len;
addr_len = sizeof(struct sockaddr);

packetIn.length=recv(lesocket, packetIn.buf, MAXBUFLEN, 0);
return packetIn.length;
}

hope this help :/
Ok this problem only occurs in localhost with my pc.
with an other pc or over a lan it works perfectly
Ok the problem is resolved, the fault was mine lol.

Another question now:
I send for exemple a int like this :

(char*)&IntToSend

but is this int isn''t code on 4 bytes, if it can be coded on 1 byte, its code on one byte

How to always code the ints on 4 bytes?

Thx ;-)
bump! ^^
is my question so dumb that noone answer it? :D
When you cast an int to char*, and send it as zero-terminated string, then you won''t be sending the entire int. For example, if the value you want to send is 0x120034, then the only byte you will receive is 0x34, because the 00 byte in the middle will be interpreted as a terminator.

The reason for this is that you use strlen(). Use an alternate version of your send and receive functions that take a void* and a length, and you can send an int like so:

  send( &theInt, sizeof( theInt ) ); 


Beware byte order issues when involving machines other than PC hardware, though.

Also, if you''re using TCP, there''s no guarantee that you''ll get the same number of bytes in one read on the other end as you sent. The network may split a packet in two, or may coalesce packets. Sometimes, only after packet loss, so it''s hard to emulate, and only happens intermittently. Thus, you need to preceed each packet of data you want to send by a length. Search this forum for "packetizing tcp stream" or something similar.

If you use UDP, that caveat doesn''t apply.
enum Bool { True, False, FileNotFound };
Both send and recv take a length. Assuming you''re using blocking I/O, the call will block until that many bytes have been received or sent. So i don''t really see what the problem is. An int is always 4 bytes (assuming an int is 4 bytes on the machine you''re using) regardless of the size of an int. In this code:

int x = 4;
int y = 65000;

Both x and y are taking up 4 bytes of memory. If you send sizeof(int) bytes, you''ll send 4 bytes. It doesn''t matter what they hold.
Thx, it''s working now ;-)

This topic is closed to new replies.

Advertisement