Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Pegasus

Send() & Recv() : different size?

This topic is 5226 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Post the code that assembles the string into char or byte before calling recv(), WSARecv(), etc.

Kuphryn

Share this post


Link to post
Share on other sites
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 :/

Share this post


Link to post
Share on other sites
Ok this problem only occurs in localhost with my pc.
with an other pc or over a lan it works perfectly

Share this post


Link to post
Share on other sites
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 ;-)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!