Sockets read or recv

  • Follow


Hi,  When doing simple socket reads should I use  read()  or actually 
use recv()  (with the flags value set to zero).  I believe they'll do 
exactly the same thing?   Which one is the preferred style?  Is there 
any consideration that I'm missing?

Thanks,

MC
0
Reply spamspamspam3 (19) 7/21/2011 9:15:20 PM

In <4e2896ed$0$35553$c3e8da3$4db35a27@news.astraweb.com> Martin Crouch <spamspamspam@spaml.com> writes:

> Hi,  When doing simple socket reads should I use  read()  or actually 
> use recv()  (with the flags value set to zero).  I believe they'll do 
> exactly the same thing?   Which one is the preferred style?  Is there 
> any consideration that I'm missing?

If you need the extra control that the "flags" argument provides, then
you must (obviously) use recv().

If you don't, and would therefore set "flags" to zero, then the two methods
are identical.  In that case I would assume the preferred style is to use
read(), which more clearly broadcasts your intentions to whomever is
reading the code.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

0
Reply gordon16 (617) 7/21/2011 9:54:33 PM


Martin Crouch <spamspamspam@spaml.com> writes:
> Hi,  When doing simple socket reads should I use  read()  or actually
> use recv()  (with the flags value set to zero).  I believe they'll do
> exactly the same thing?   Which one is the preferred style?

There is presumably no such thing as 'a preferred style'. My opinion
on that would be that recv is less universal (only for sockets) and
more complicated (because of the flags argument) than read and because
of this, I think using read where it is sufficient should be preferred
over using recv where it isn't needed.
0
Reply rweikusat (2678) 7/21/2011 10:03:03 PM

On Thu, 21 Jul 2011 22:15:20 +0100, Martin Crouch wrote:

> Hi,  When doing simple socket reads should I use  read()  or actually use
> recv()  (with the flags value set to zero).  I believe they'll do exactly
> the same thing?   Which one is the preferred style?  Is there any
> consideration that I'm missing?

There's no reason to use recv() without flags. You may as well just use
read(). Or use fdopen() and stdio.

Not using recv() has the advantage that portions of the code may work on
something other than a socket.

0
Reply nobody (4804) 7/22/2011 3:35:26 PM

Nobody <nobody@nowhere.com> wrote:

> On Thu, 21 Jul 2011 22:15:20 +0100, Martin Crouch wrote:
>
>> Hi,  When doing simple socket reads should I use  read()  or actually use
>> recv()  (with the flags value set to zero).  I believe they'll do exactly
>> the same thing?   Which one is the preferred style?  Is there any
>> consideration that I'm missing?
>
> There's no reason to use recv() without flags. You may as well just use
> read(). Or use fdopen() and stdio.
>
> Not using recv() has the advantage that portions of the code may work on
> something other than a socket.
>

Using recv() and send() makes it easier to port the code to winsock (at
least this used to be the case, things may have changed).

-- 
Huibert
"Okay... really not something I needed to see."  --Raven
0
Reply huibert.bol (74) 7/22/2011 5:20:30 PM

Huibert Bol <huibert.bol@quicknet.nl> writes:
> Nobody <nobody@nowhere.com> wrote:
>> On Thu, 21 Jul 2011 22:15:20 +0100, Martin Crouch wrote:
>>
>>> Hi,  When doing simple socket reads should I use  read()  or actually use
>>> recv()  (with the flags value set to zero).  I believe they'll do exactly
>>> the same thing?   Which one is the preferred style?  Is there any
>>> consideration that I'm missing?
>>
>> There's no reason to use recv() without flags. You may as well just use
>> read(). Or use fdopen() and stdio.
>>
>> Not using recv() has the advantage that portions of the code may work on
>> something other than a socket.
>>
>
> Using recv() and send() makes it easier to port the code to winsock (at
> least this used to be the case, things may have changed).

Holy contagious contortion ...
0
Reply rweikusat (2678) 7/22/2011 6:49:08 PM

Huibert Bol <huibert.bol@quicknet.nl> wrote:
> Nobody <nobody@nowhere.com> wrote:

> > On Thu, 21 Jul 2011 22:15:20 +0100, Martin Crouch wrote:
> >
> >> Hi,  When doing simple socket reads should I use  read()  or actually use
> >> recv()  (with the flags value set to zero).  I believe they'll do exactly
> >> the same thing?   Which one is the preferred style?  Is there any
> >> consideration that I'm missing?
> >
> > There's no reason to use recv() without flags. You may as well just use
> > read(). Or use fdopen() and stdio.
> >
> > Not using recv() has the advantage that portions of the code may work on
> > something other than a socket.
> >

> Using recv() and send() makes it easier to port the code to winsock (at
> least this used to be the case, things may have changed).

That's what I thought. But now my socket code uses #if _WIN32 because being
able to use pipes made writing regression tests much easier. I could, for
example, compile an asynchronous network protocol library into a shell
utility and feed it pathological cases from a script. No need to bother with
bootstrapping a sockets environment; even socketpair() would be infinitely
more bothersome.
0
Reply William 7/22/2011 9:40:12 PM

On Jul 22, 8:35=A0am, Nobody <nob...@nowhere.com> wrote:

> Not using recv() has the advantage that portions of the code may work on
> something other than a socket.

Of course, if your code can't work on anything but sockets for some
other reason, using 'recv()' clearly broadcasts this.

DS
0
Reply David 7/22/2011 10:14:48 PM

Rainer Weikusat <rweikusat@mssgmbh.com> wrote:
> Huibert Bol <huibert.bol@quicknet.nl> writes:
> > Using recv() and send() makes it easier to port the code to
> > winsock (at least this used to be the case, things may have
> > changed).

> Holy contagious contortion ...

http://www.netperf.org/ ...  although I'm not sure if the WIN32 stuff
actually still compiles presently.

rick jones
entertains the notion of a "native API" version for netperf WIN32...
-- 
a wide gulf separates "what if" from "if only"
these opinions are mine, all mine; HP might not want them anyway... :)
feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH...
0
Reply rick.jones2 (1061) 7/22/2011 10:21:43 PM

David Schwartz <davids@webmaster.com> writes:
> On Jul 22, 8:35�am, Nobody <nob...@nowhere.com> wrote:
>> something other than a socket.
>
> Of course, if your code can't work on anything but sockets for some
> other reason, using 'recv()' clearly broadcasts this.

But except if the flags argument to recv is actually used, the part of
the code which deals with receiving the data _can_ work on all kinds
of file descriptors.
0
Reply rweikusat (2678) 7/22/2011 10:35:44 PM

Martin Crouch wrote:
> Hi,  When doing simple socket reads should I use  read()  or actually
> use recv()  (with the flags value set to zero).  I believe they'll do
> exactly the same thing?   Which one is the preferred style?  Is there
> any consideration that I'm missing?
>
> Thanks,
>
> MC

    Here's a nice tutorial.

http://beej.us/guide/bgnet/output/html/multipage/index.html

Unix socket programming is a good book too. Great book! rec() depends 
somewhat on the type of sockets your using. TCP or UDP.

Bill


0
Reply nospam116 (1187) 7/28/2011 5:27:01 PM

10 Replies
69 Views

(page loaded in 0.221 seconds)

Similiar Articles:













7/29/2012 11:13:28 AM


Reply: