Archived

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

just a quickie noob question about recv()

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

when you are using winsock in a program and using send() / recv() functions, is it normal just to use recv() with a big static array of chars, like buffer[255]; or do most "properly" implemented programs use a dynamic array of chars for neatness? if it is the former, what happens if more data needs to be recv''d than the buffer holds? is that data just lost? thanks a lot saucers

Share this post


Link to post
Share on other sites
If you fill your buffer in with more than it can hold you will get a buffer overflow, crash, and possibly let a hacker crack into your system. You want to have a large size buffer, fill it up, then dump it out to a dynamic one, fill it up again with the rest, and dump that too, and so on until you have the entire packet.

Share this post


Link to post
Share on other sites
It's perfectly fine to use a static buffer, as long as you tell recv() the size of that buffer. recv() will not overflow the buffer you hadn it, assuming you don't lie to it. If your protocol never sends packets bigger than 255 bytes, it's fine to pass a 255 byte static buffer to recv(). If someone else sends you a bigger packet, the kernel will give you the first 255 bytes, and the rest will be lost -- no overwrite worries.

EDIT: clarify that malicious packets can't bite you if you don't lie to recv().

[edited by - hplus0603 on February 22, 2004 7:21:52 PM]

Share this post


Link to post
Share on other sites