5.1.2 Packet Format

4. listen for connections (TCP only);
5. accept connections (TCP only); and
6. send and receive data.
Creating a local server socket is no different from creating a local client socket. Selecting a port number, and making it known to clients is discussed above. Once a port number is selected, the server must call bind to associate the address with the socket. The caller must specify a complete address, including both the port number and IP address in the sockaddr_in structure. Usually, the IP address is set to the constant htonl(INADDR_ANY), which indicates that the socket should accept any connections (or datagrams for SOCK_DGRAM sockets) destined for any IP address associated with the host. (Recall that machines often have several IP addresses.) Other possibilities are htonl(INADDR_LOOPBACK), or a value obtained from gethostname, gethostbyname, etc.
Communication with recvfrom and sendto Once a SOCK_DGRAM socket is bound to an address, it is ready to send and receive datagrams. Read and write may be used to communicate with clients. The recvfrom call is particularly useful for servers because in addition to the contents of the datagram, it also supplies the caller with a return address, suitable for use in a subsequent call to sendto.
Listening for and accepting connections SOCK_STREAM sockets, on the other hand, must take a few more steps before they are ready for use. First, they must call listen to inform the OS of their intention to accept connections. The accept system call allocates a new file descriptor which can be used with read and write to communicate with the foreign entity. In addition, accept supplies the caller with the address of the connecting entity.
Many of the design choices for server software architecture are concerned with the detailed behavior of accept. It can be made blocking or non-blocking, and upon acceptance of a connection, a new thread or process may or may not be created. Signals (including timer signals) may be used to force a premature return and select can be used to learn about status changes. The large number of possibilities tends to make servers much more complex than clients.
5.5 Higher Level Protocols
Sockets form the lowest layer of user-level network programming. If you go any lower, you enter the realm of driver-writing and operating system internals. Most Beowulf users don't write applications using sockets. Sockets are usually reserved

 



How to Build a Beowulf
How to Build a Beowulf: A Guide to the Implementation and Application of PC Clusters (Scientific and Engineering Computation)
ISBN: 026269218X
EAN: 2147483647
Year: 1999
Pages: 134

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