listen Function

 < Day Day Up > 



listen Function

The listen function is used for stream sockets to mark a socket as willing to accept incoming connections. This function may only be called for sockets initially created of type SOCK_STREAM or SOCK_SEQPACKET. The listen function has the following prototype:

        #include <sys/socket.h>         int listen( int sock, int backlog );

The sock argument is a previously created socket (of the previously defined type) and backlog represents the size of the queue that will hold accepted connections.

Note 

Some systems define the backlog as the size of the queue for incomplete connections, whereas others define it as the size for completed connections (still others split backlog evenly for two queues). An incomplete connection is one that has not yet completed the TCP three-way handshake (for SOCK_STREAM connections). Complete connections are those that have completed the TCP three-way handshake. The TCP three-way handshake is a synchronization step in which a client sends a SYN packet, the server responds with a SYN/ACK packet, and then the client responds with an ACK. Also performed during this state is the communication of parameters that are to be used for the connection (such as time stamp, maximum size of segments, and so on).

The listen function is required to be performed before an accept function is possible on a server socket (also known as a passive socket in this state). What’s important to note is that no data is ever communicated through a passive socket; all that is permitted is the acceptance of new client sockets.

The backlog parameter is often defined as the number 5 in many applications, but this value ultimately depends upon the number of incoming connections that are desired to be queued for accept at any given time. Consider an HTTP server that specified a value of 5. With a large load (thousands of connections per second), many of these connections would be refused by the server, as there would be no room on the backlog queue awaiting accepts by the server. For this reason, many HTTP servers specify very large values to the listen function to ensure a sufficient queue for connection accept.



 < Day Day Up > 



BSD Sockets Programming from a Multi-Language Perspective
Network Programming for Microsoft Windows , Second Edition (Microsoft Programming Series)
ISBN: 1584502681
EAN: 2147483647
Year: 2003
Pages: 225
Authors: Jim Ohlund

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net