Advertisement

just a quickie noob question about recv()

Started by February 21, 2004 07:09 PM
1 comment, last by Saucers 20 years, 11 months ago
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
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.
____________________________________________________________AAAAA: American Association Against Adobe AcrobatYou know you hate PDFs...
Advertisement
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]
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement