Archived

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

Packet Size

This topic is 5335 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

My main question is what type of problems do you run into if you start tyring to send and recieve UDP packets that transfer data smaller than the designated buffer length. Example: char text[8] = "xx xx xx xx"; sendto(socket, text, 32, 0, (struct sockaddr*)addr, &addr_size); And on the other end: char text[8]; recvfrom(socket, text, 32, 0, (struct sockaddr*)addr, &addr_size); Notice that only a buffer of size 8 is being transfered, but the designated length is 32. Is this a bad idea? Why?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by MrShmoo Notice that only a buffer of size 8 is being transfered,

Nope, 32 bytes. And recvfrom() will crash your app.

Share this post


Link to post
Share on other sites
You''ll get a segmentation fault, because the memory you point to is out of the boundaries of the array (text) and the OS didn''t allocate it for you (you never asked it to ).

"I''ll be Bach!" (c) Johann Sebastian Schwarzenegger

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The libraries don''t know the size of your data. They''ll happily read 32 bytes from the start of "text" in sendto(). This may crash, or it may just send whatever junk is on the stack after "text" (which could be, say, the return address for the function stack frame).

The recvfrom() function will, similarly, write 32 bytes into whatever memory starts at "text", and then overwrite whatever 24 bytes comes after that on the stack (or in global memory, if text is a global or static).

I suggest stopping your program in the debugger at the point of sendto() and recvfrom(), and using "view memory" using the address of text, and inspect the bytes around it.

Either, you''ll now have an "aha!" experience, and start realizing how memory and pointers work, or you''ll think it''s all hard, which means that you''ll never be good with pointers, and might just want to stick with Flash animations for the rest of your life.

Share this post


Link to post
Share on other sites