Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

read(), readv() - receive a message from a socket

&pagelevel(4)&pagelevel

#include <sys/socket.h>
#include <sys/uio.h>

ssize_t read(int s, char *buf, int len);
ssize_t readv(int s, const struct iovec *iov, int iovcnt);

Description

The read() and readv() functions receive messages from a socket. read() and readv() can only be used with a socket over which a connection is set up. The length of the message is returned.

More general information on this function is available in the manual "C Library Functions for POSIX Applications".

The s parameter designates the socket from which the message is to be received.

For read(), the buf parameter points to the first byte of the receive buffer. The len parameter specifies the length (in bytes) of the receive buffer, and thus the maximum message length.

For readv(), the received data is placed in a vector (array) with the members iov[0] to iov[iovcnt-1]. The vector members are objects of the type struct iovec. The address of the vector is passed in the iov parameter. Each vector member contains the address and length of a storage area into which readv() stores the data received from socket s. readv() fills one area after the other with data, with readv() always moving on to the next area only when the current area is completely filled with data.

The iovec structure is declared as follows:

struct iovec {
  caddr_t  iov_base;  /* buffer for data */
  size_t   iov_len;   /* length of buffer */
};

iovcnt indicates the number of vector members.

If no messages are available on the socket, the receive call waits for an incoming message, unless the socket is non-blocking (refer to section "ioctl() - control sockets"). In that case, read() and readv() return the value -1, and the errno variable is set to the value EWOULDBLOCK.

The poll() or select() function can be used to determine when further data arrives.

If the process which calls read() or readv() receives a signal before any data is available, the function concerned is called again in a standard case. The function is not called again if the calling process has specified with sigaction() to interrupt these function calls (see also the manual "C Library Functions for POSIX Applications").

Return value

>0:

If successful.

-1:

If errors occur. errno is set to indicate the error.

Errors

EBADF

The s parameter is not a valid descriptor.

ECONNRESET

The connection to the partner was interrupted (only with type SOCK_STREAM sockets).

EFAULT

The data is to be received in a non-existent or protected part of the process address range.

EINTR

The calling process has received a signal before any data could be received. The setting to interrupt the function call and not repeat it is active.

EIO

User data has been lost.

ENETDOWN

The connection to the network is down.

ENOTCONN

No connection exists for the socket.

ENOTSOCK

Descriptor s references a file and not a socket.

EWOULDBLOCK

The socket is marked as non-blocking and the requested operation would block.

See also

connect(), getsockopt(), recv(), send(), socket(), fcntl(), ioctl(), select(), write()