recv()--Receive Data


  BSD 4.3 Syntax
  #include <sys/types.h>
  #include <sys/socket.h>

 int recv(int socket_descriptor,
         char *buffer,
         int buffer_length,
         int flags)

  Service Program Name: QSOSRV1

  Default Public Authority: *USE

  Threadsafe: Yes



  UNIX® 98 Compatible Syntax
  #define _XOPEN_SOURCE 520
  #include <sys/socket.h>

 ssize_t recv(int socket_descriptor,
         void *buffer,
         size_t buffer_length,
         int flags)

  Service Program Name: QSOSRV1

  Default Public Authority: *USE

  Threadsafe: Yes


The recv() function is used to receive data through a socket.

There are two versions of the API, as shown above. The base IBM® i API uses BSD 4.3 structures and syntax. The other uses syntax and structures compatible with the UNIX 98 programming interface specifications. You can select the UNIX 98 compatible interface with the _XOPEN_SOURCE macro.


Parameters

socket_descriptor
(Input) The socket descriptor that is to be read from.

buffer
(Input) The pointer to the buffer in which the data that is to be read is stored.

buffer_length
(Input) The length of the buffer.

flags
(Input) A flag value that controls the reception of the data. The flags value is either zero, or is obtained by performing an OR operation on one or more of the following constants:

Authorities

No authorization is required.


Return Value

recv() returns an integer. Possible values are:


Error Conditions

When recv() fails, errno can be set to one of the following:



Error Messages



Usage Notes

  1. For sockets that use a connection-oriented transport service (for example, sockets with a type of SOCK_STREAM), a returned value of zero indicates one of the following:
    • The partner program has issued a close() for the socket.

    • The partner program has issued a shutdown() to disable writing to the socket.

    • The connection is broken and the error was returned on a previously issued socket function.

    • A shutdown() to disable reading was previously done on the socket.


  2. The following applies to sockets that use a connectionless transport service (for example, a socket with a type of SOCK_DGRAM):
    • If a connect() has been issued previously, then data can be received only from the address specified in the previous connect().

    • The address from which data is received is discarded, since the recv() has no address parameter.

    • The entire message must be read in a single read operation. If the size of the message is too large to fit in the user supplied buffer, the remaining bytes of the message are discarded.

    • A returned value of zero indicates one of the following:
      • The partner program has sent a NULL message (a datagram with no user data),

      • A shutdown() to disable reading was previously done on the socket.

      • The buffer length specified was zero.

  3. When you develop in C-based languages and an application is compiled with the _XOPEN_SOURCE macro defined to the value 520 or greater, the recv() API is mapped to qso_recv98().

Related Information



API introduced: V3R1