recv

Receives bytes from a connected socket and places them into a buffer.

Format

#include  <socket.h>

int recv  (int s, char *buf, int len, int
          flags);

Arguments

s
A socket descriptor that was created as the result of a call to accept or connect.
buf
A pointer to a buffer into which received data will be placed.
len
The size of the buffer pointed to by buf.
flags
A bit mask that may contain one or more of MSG_OOB and MSG_ PEEK. It is built by ORing the appropriate values together.

The MSG_OOB flag allows out-of-band data to be received. If out-of- band data is available, it will be read before any other data that is available. If no out-of-band data is available, the MSG_OOB flag is ignored. Out-of-band data can be sent using send, sendmsg, and sendto.

The MSG_PEEK flag allows you to peek at the data that is next in line to be received without removing it from the system's buffers.

Description

This routine receives data from a connected socket. To receive data on an unconnected socket, use the recvfrom or recvmsg routines. The received data is placed in the buffer buf.

Data is sent by the socket's peer using the send, sendmsg, or sendto routines.

You may use the select routine to determine when more data arrives.

If no data is available at the socket, the receive call waits for data to arrive, unless the socket is nonblocking in which case a -1 is returned with the external variable errno set to EWOULDBLOCK.

See also read, send, sendmsg, sendto, and socket in this section.

Return Values
The number of bytes received and placed in buf
Indicates that a connection is broken or reset by its peer. 
-1  Indicates an error; errno is set to one of the following:

  • EBADF - The socket descriptor is invalid.

  • ENOTSOCK - The descriptor references a file, not a socket.

  • EPIPE - An attempt was made to write to a socket that is not open for reading by any process.

  • EWOULDBLOCK - The NBIO socket option (nonblocking) flag is set for the socket or file descriptor and the process would be delayed in the read operation.

  • EFAULT - The data was specified to be received into a non-existent or protected part of the process address space.
 


Previous Page | Next Page | Table of Contents | Index