recv(2) with a argument of flag of literal 0 - Unix

This is a discussion on recv(2) with a argument of flag of literal 0 - Unix ; ssize_t recv(int socket, void *buffer, size_t length, int flags); flags MSG_PEEK, MSG_OOB, MSG_WAITALL The recv() function is identical to recvfrom() .., and to read() if no flags are used. The code examples in some books and posts in this group ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: recv(2) with a argument of flag of literal 0

  1. recv(2) with a argument of flag of literal 0


    ssize_t recv(int socket, void *buffer, size_t length, int flags);

    flags
    MSG_PEEK, MSG_OOB, MSG_WAITALL

    The recv() function is identical to recvfrom() .., and to read() if no
    flags are used.


    The code examples in some books and posts in this group just use the
    literal `0' as the `flags' argument of recv(). This value of `0' isn't
    in the value set of flags, so is this ok? Don't we have a MSG_NULL
    option?

    Thanks.

  2. Re: recv(2) with a argument of flag of literal 0

    On Nov 3, 12:46*am, "lovecreatesbea...@gmail.c0m"
    wrote:
    >
    > ssize_t recv(int socket, void *buffer, size_t length, int flags);
    >
    > flags
    > MSG_PEEK, MSG_OOB, MSG_WAITALL
    >
    > The recv() function is identical to recvfrom() .., and to read() if no
    > flags are used.
    >

    >
    > The code examples in some books and posts in this group just use the
    > literal `0' as the `flags' argument of recv(). This value of `0' isn't
    > in the value set of flags, so is this ok? Don't we have a MSG_NULL
    > option?
    >
    > Thanks.


    There is no defined 'no flags' constant, so zero is used.

    DS

  3. Re: recv(2) with a argument of flag of literal 0

    Hi

    On Mon, 03 Nov 2008 00:46:28 -0800, lovecreatesbeauty@gmail.c0m wrote:

    > The code examples in some books and posts in this group just use the
    > literal `0' as the `flags' argument of recv(). This value of `0' isn't
    > in the value set of flags, so is this ok? Don't we have a MSG_NULL
    > option?


    According to Linux man recv:

    "The flags argument to a recv() call is formed by OR'ing one or more of
    the following values: ..."

    According to SUSv3: "flags Specifies the type of message reception.
    Values of this argument are formed by logically OR'ing zero or more of
    the following values: ..."

    So 0 is a legal value in supposedly-portable code, but it might not work
    on Linux.

    I suspect that the Linux documentation is just wrong and that Linux does
    allow 0 as required by POSIX/SUS, but I haven't tested it.

    viza

  4. Re: recv(2) with a argument of flag of literal 0

    On Nov 3, 1:00*am, viza
    wrote:

    > According to Linux man recv:
    >
    > "The *flags argument to a recv() call is formed by OR'ing one or more of
    > *the following values: ..."
    >
    > According to SUSv3: "flags Specifies the type of message reception.
    > Values of this argument are formed by logically OR'ing zero or more of
    > the following values: ..."
    >
    > So 0 is a legal value in supposedly-portable code, but it might not work
    > on Linux.


    > I suspect that the Linux documentation is just wrong and that Linux does
    > allow 0 as required by POSIX/SUS, but I haven't tested it.


    Linux does allow 0 as required by POSIX /SUS. The SUS wording is
    awful, but precise. The Linux wording is nice, but imprecise.

    DS

  5. Re: recv(2) with a argument of flag of literal 0

    On Nov 3, 5:00 pm, viza
    wrote:
    >
    > On Mon, 03 Nov 2008 00:46:28 -0800, lovecreatesbea...@gmail.c0m wrote:
    > > The code examples in some books and posts in this group just use the
    > > literal `0' as the `flags' argument of recv(). This value of `0' isn't
    > > in the value set of flags, so is this ok? Don't we have a MSG_NULL
    > > option?

    >
    > According to SUSv3: "flags Specifies the type of message reception.
    > Values of this argument are formed by logically OR'ing zero or more of
    > the following values: ..."
    >


    In my understanding, it means the count of the values of flags can be
    zero or more (with OR'ing MSG_PEEK, MSG_OOB, MSG_WAITALL).

    > So 0 is a legal value in supposedly-portable code, but it might not work
    > on Linux.
    >


    If `0' was passed to recv as the flags argument, the count of value of
    that flags argument is ONE. If SUS states that it accepts a NULL / 0
    for flags of recv(), it is ok.

  6. Re: recv(2) with a argument of flag of literal 0

    "lovecreatesbeauty@gmail.c0m" writes:

    >
    > ssize_t recv(int socket, void *buffer, size_t length, int flags);
    >
    > flags
    > MSG_PEEK, MSG_OOB, MSG_WAITALL
    >
    > The recv() function is identical to recvfrom() .., and to read() if no
    > flags are used.
    >

    >
    > The code examples in some books and posts in this group just use the
    > literal `0' as the `flags' argument of recv(). This value of `0' isn't
    > in the value set of flags, so is this ok? Don't we have a MSG_NULL
    > option?


    It's a usual convention to use 0, without a special name, in cases like
    this. Since each flag normally corresponds to a bit in the flags
    argument, the value with no flags set is certainly going to be zero.

    It may help if you realize that the kernel code for recv() generally
    does something like:

    if (flags & MSG_PEEK) { ... }
    if (flags & MSG_OOB) { ... }
    if (flags & MSG_WAITALL) { ... }

    If flags is 0, all these tests will be false, which is what you want.

+ Reply to Thread