Sockets Programming Interfaces
3-22
Listen for Connection Requests on Socket
listen
Syntax
int listen( int s, int maxcon );
Parameter(s)
s
Socket
maxcon
Maximum number of connects to queue
Return Value
If it succeeds, the function returns 0. Otherwise, a value of –1 is returned and
the function fdError() can be called to determine the error:
EBADF
The file descriptor (socket) is invalid.
ENOTSOCK
The file descriptor does not reference a socket.
EOPNOTSUPP
The socket is not of a type that supports the
operation listen.
EISCONN
The socket is already connected.
Description
The listen() function listens for connection requests on a socket.
To accept connections, a socket is first created with socket(), The listen() func-
tion is called to specify a willingness to accept incoming connections and a
queue limit for incoming. New connections are accepted by calling the accept()
function. The listen() function applies only to sockets of type SOCK_STREAM.
The maxcon parameter defines the maximum length the queue of pending
connections may grow to. If a connection request arrives with the queue full,
the client receives an error with an indication of ECONNREFUSED.
Receive Data from a Socket
recv
Syntax
int recv( int s, void *pbuf, int size, int flags );
Parameter(s)
s
Socket
pbuf
Data buffer to place received data
size
Size of desired data
flags
Option flags
Return Value
If it succeeds, the function returns the number of bytes received. Otherwise,
a value of –1 is returned and the function fdError() can be called to determine
the error:
EBADF
The file descriptor (socket) is invalid.
ENOTSOCK
The file descriptor does not reference a socket.