Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Buffered Recv Function?

Buffered Recv Function?

Buffered Recv Function?


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?


RE: Buffered Recv Function?

My man page says that recv 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.

RE: Buffered Recv Function?

I would think so too, but I have


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)

RE: Buffered Recv Function?

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.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close