#include <sys/socket.h>
|
int
listen (int s, int backlog); |
The backlog argument defines the maximum length the queue of pending connections may grow to. The real maximum queue length will be 1.5 times more than the value specified in the backlog argument. A subsequent listen system call on the listening socket allows the caller to change the maximum queue length using a new backlog argument. If a connection request arrives with the queue full the client may receive an error with an indication of ECONNREFUSED, or, in the case of TCP, the connection will be silently dropped.
Note that before BSD 4.5 and the introduction of the syncache, the backlog argument also determined the length of the incomplete connection queue, which held TCP sockets in the process of completing TCP’s 3-way handshake. These incomplete connections are now held entirely in the syncache, which is unaffected by queue lengths. Inflated backlog values to help handle denial of service attacks are no longer necessary.
#include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> void listen_example() { int sock_fd; int newsock_fd; struct sockaddr_in addr; struct sockaddr_in ss; struct sockaddr_in new_socket; unsigned int len; unsigned int addr_len; sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(5000); bind(sock_fd,(struct sockaddr*)&addr,sizeof(addr)); listen(sock_fd,1); close(sock_fd); }
[EBADF] | |
The argument s is not a valid descriptor. | |
[EINVAL] | |
The socket is already connected, or in the process of being connected. | |
[ENOTSOCK] | |
The argument s is not a socket. | |
[EOPNOTSUPP] | |
The socket is not of a type that supports the operation listen. | |
The listen system call appeared in BSD 4.2. The ability to configure the maximum backlog at run-time, and to use a negative backlog to request the maximum allowable value, was introduced in 2.2.
Feedback
For additional information or queries on this page send feedback
© 2005-2007 Nokia |