概述
NAME
listen - listen for socket connections and limit the queue of incoming connections
SYNOPSIS
#include <sys/socket.h>
int listen(int socket, int backlog);
DESCRIPTION
The listen() function shall mark a connection-mode socket, specified by the socket argument, as accepting connections.
The backlog argument provides a hint to the implementation which the implementation shall use to limit the number of outstanding connections in the socket's listen queue. Implementations may impose a limit on backlog and silently reduce the specified value. Normally, a larger backlog argument value shall result in a larger or equal length of the listen queue. Implementations shall support values of backlog up to SOMAXCONN, defined in <sys/socket.h>.
The implementation may include incomplete connections in its listen queue. The limits on the number of incomplete connections and completed connections queued may be different.
The implementation may have an upper limit on the length of the listen queue-either global or per accepting socket. If backlog exceeds this limit, the length of the listen queue is set to the limit.
If listen() is called with a backlog argument value that is less than 0, the function behaves as if it had been called with a backlog argument value of 0.
A backlog argument of 0 may allow the socket to accept connections, in which case the length of the listen queue may be set to an implementation-defined minimum value.
The socket in use may require the process to have appropriate privileges to use the listen() function.
RETURN VALUE
Upon successful completions, listen() shall return 0; otherwise, -1 shall be returned and errno set to indicate the error.
listen()
mark a socket as accepting connections
SYNOPSIS
#include <sys/socket.h>
int listen(int s, int backlog);
DESCRIPTION
The listen() function marks a connection-mode socket (for example, those of type SOCK_STREAM), specified by the socket argument s, as accepting connections, and limits the number of outstanding connections in the socket's listen queue to the value specified by the backlog argument. The socket s is put into 'passive' mode where incoming connection requests are acknowledged and queued pending acceptance by the process.
The backlog parameter of this function is typically used by servers that could have more than one connection request at a time: if a connection request arrives with the queue full, the client receives an error with an indication of ECONNREFUSED.
listen() attempts to continue to function rationally when there are no available descriptors. It accepts connections until the queue is emptied. If descriptors become available, a later call to listen() or accept() re-fills the queue to the current or most recent backlog', if possible, and resume listening for incoming connections.
An application may call listen() more than once on the same socket. This has the effect of updating the current backlog for the listening socket. Should there be more pending connections than the new backlog value, the excess pending connections are reset and dropped.
backlog is limited (silently) to a reasonable value as determined by the underlying service provider. Illegal values are replaced by the nearest legal value.
PARAMETERS
s
Is a descriptor identifying a bound, unconnected socket.
backlog
Is the maximum length that the queue of pending connections may grow to. If this value is SOMAXCONN, then the underlying service provider responsible for socket s sets the backlog to a maximum "reasonable" value.
RETURN VALUES
If successful, listen() returns a value of zero. On failure, it returns -1 and sets errno to one of the following values:
EBADF
s is not a valid descriptor
EINCONN
s is already connected.
EINVAL
s has not been bound to a local address.
EMFILE
No more socket descriptors are available.
ENETDOWN
Network subsystem has failed.
ENOBUFS
No buffer space is available.
ENOTSOCK
s is not a socket
EOPNOTSUPP
s is not a socket that supports listen().
The listen Function:
The listen function is called only by a TCP server and it performs two actions:
The listen function converts an unconnected socket into a passive socket, indicating that the kernel should accept incoming connection requests directed to this socket.
The second argument to this function specifies the maximum number of connections the kernel should queue for this socket.
#include <sys/types.h>
#include <sys/socket.h>
int listen(int sockfd,int backlog);
This call returns 0 on success otherwise it gives you -1 on error.
Parameters:
sockfd: is a socket descriptor returned by the socket function.
backlog is the number of allowed connections.
最后
以上就是眯眯眼百合为你收集整理的linux网络编程之listen函数的全部内容,希望文章能够帮你解决linux网络编程之listen函数所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复