[cs631apue] Best way to read from a socket

Jan Schaumann jschauma at stevens.edu
Fri Dec 6 22:09:05 EST 2013


lbustama <lbustama at stevens.edu> wrote:
> Hi, Just wondering what the best way to read from a socket is?
>
> Do we have to read from the socket character by character until a '/r'  
> and '/n' is found?  is there a better way?

Just read data into a reasonably sized buffer.  You should have an idea
for how long a reasonable request might be, and you know that read(2)
will happily return fewer than the requested number of bytes if there
are fewer bytes available in the input.

You can also use recv(2), for example.

Once you have a buffer of data, you can then search for the relevant
delimiters and decide if you need to read more data or not.

-Jan


More information about the cs631apue mailing list