accept Function

 < Day Day Up > 



accept Function

The accept function is used by a server application (using a socket that previously called listen) to accept incoming client connections. These client connections are dequeued from the accepted connection queue (see Figure 3.4). If the queue is empty, the accept function blocks, unless the server socket was previously defined as nonblocking. The accept function has the following prototype:

    #include <sys/types.h>     #include <sys/socket.h>     int accept(int sock, struct sockaddr *addr, socklen_t *addrlen);

click to expand
Figure 3.4: Graphical depiction of the accept function.

When a new connection is queued in the accepted connection queue, the accept function unblocks and returns the new socket to the caller. Sockets on the accepted connection queue are returned in First In, First Out (FIFO) order.

The calling application provides a server socket for which the listen function was previously invoked. The caller also provides a sockaddr_in structure (because we’re operating within the AF_INET domain) and an int pointer (socklen_t) that identifies the size of the returned addr structure. The address structure returned within the accept function defines the host and port of the remote client that was accepted by the server.

Now, let’s look at an example of the accept function (see Listing 3.4). This application demonstrates the accept function, including all of the necessary elements that must precede it (such as listen and bind). When the accept function in Listing 3.4 returns, a new client socket will be contained within connectionFd, and the remote host and port will be contained within the address structure cliaddr.

Listing 3.4 Sample code example for the accept function.

start example
int serverFd, connectionFd, clilen; struct sockaddr_in servaddr, cliaddr; serverFd = socket( AF_INET, SOCK_STREAM, 0 ); memset( &servaddr, 0, sizeof(servaddr) ); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl( INADDR_ANY ); servaddr.sin_port = htons( MY_PORT ); bind( serverFd, (struct sockaddr *)&servaddr, sizeof(servaddr) ); listen( serverFd, 5 ); clilen = 0; connectionFd = accept( serverFd,                          (struct sockaddr_in *)&cliaddr, &clilen );
end example

If the server did not require information about the client, a NULL could be passed for the second and third parameters to force the accept function to avoid passing them back. For example:

    connectionFd = accept( serverFd,                                  (struct sockaddr_in *)NULL, NULL );

Note that if the server application required this information, it could use the getpeername function on the connectionFd to identify the remote host and port. We look at the getpeername function in Chapter 4, Advanced Sockets Functions.



 < 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