Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Buffered Recv Function?

Status
Not open for further replies.

wb17

Programmer
Feb 17, 2009
2
CA
Hi,

When using recv if the len parameter is shorter than the data in the buffer the remaining data is discarded. This seems strange to me, I thought I was reading from a stream? Is there anyway to read a fixed number of bytes and not loose the rest?

I know I can create my own buffer to read data from recv into and then go through the data in my buffer in pieces. But now recv takes data from some internal buffer, I read it into my own buffer, then look at pieces of that and divide them into their own buffers for processing. Isn't that at least one buffer too many? And I'll need a buffer for each client, which will start to eat up a lot of memory.

If I have to make my own buffer, how big does it need to be so that I don't loose data when calling recv?

Thanks!
 
My man page says that [tt]recv[/tt] may discard excess bytes depending on the type of socket. In other words, it'll probably discard excess bytes if you use it on a datagram socket, but for a stream socket, it shouldn't discard a thing.
 
I would think so too, but I have

Code:
int sock = socket(AF_INET, SOCK_STREAM, 0);

and it still discards anything bigger than the buffer. I've rewritten my code to use a whole bunch of buffers now. But still, I feel like I've missed something that would have made this a whole lot easier.

Anyway, is it safe to assume that the number of bytes returned by recv will always be less than 1500 bytes? (I'm thinking of the MTU here)
 
hi,
probably you have to use asynchronous sockets:

the system calls you to say that something is arrived:

you read X bytes from it, and put in a circular buffer;
if ther were still byte to read, the system calls you again
and you append them to the previous. And so on.

You can use sinchronous socket if to the other side,
there is someone that sends always a fixed number of bytes,
or sends you before every transmission, the number of bytes
that it will send to you.

For example, if you build a telnet client, you cannot use syncronous socket: the server sends data in tranches of
pre-accorded size, but you don't know how many it sends, and the last is not full.

ciao
vittorio
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top