Internet Applications And Network Programming
3.22 Sockets, Threads, And Inheritance
The socket API works well with concurrent servers. Although the details depend on the underlying operating system, implementations of the socket API adhere to the following inheritance principle:
Each new thread that is created inherits a copy of all open sockets from the thread that created it.
The socket implementation uses a reference count mechanism to control each sock-et. When a socket is first created, the system sets the socket’s reference count to 1, and the socket exists as long as the reference count remains positive. When a program creates an additional thread, the thread inherits a pointer to each open socket the pro-gram owns, and the system increments the reference count of each socket by 1. When a thread calls close, the system decrements the reference count for the socket; if the refer-ence count has reached zero, the socket is removed.
In terms of a concurrent server, the main thread owns the socket used to accept in-coming connections. When a connection request arrives, the system creates a new sock-et for the new connection, and the main thread creates a new thread to handle the con-nection. Immediately after a thread is created, both threads have access to the original socket and the new socket, and the reference count of each socket is 2. The main thread calls close for the new socket, and the service thread calls close for the original socket, reducing the reference count of each to 1. Finally, when it finishes interacting with a client, the service thread calls close on the new socket, reducing the reference count to zero and causing the socket to be deleted. Thus, the lifetime of sockets in a concurrent server can be summarized:
The original socket used to accept connections exists as long as the main server thread executes; a socket used for a specific connection exists only as long as the thread exists to handle that connection.
3.23 Summary
In the Internet, all services are supplied by applications, which use either a stream paradigm or a message paradigm to communicate. The stream paradigm guarantees to deliver a sequence of bytes in order, but can choose how many bytes to pass to a re-ceiver in each batch. The message paradigm preserves boundaries, but allows messages to be lost, duplicated, or delivered out-of-order.
The basic communication model used by network applications is known as the client-server model. A program that passively waits for contact is called a server, and a program that actively initiates contact with a server is called a client.
46 Internet Applications And Network Programming Chap. 3
Each computer is assigned a unique address, and each service, such as email or web access, is assigned a unique identifier known as a protocol port number. When a server starts, it specifies a protocol port number; when contacting a server, a client specifies the address of the computer on which the server runs as well as the protocol port number the server is using.
A single client can access more than one service, a client can access servers on multiple machines, and a server for one service can become a client for other services.
Designers and programmers must be careful to avoid circular dependencies among servers.
An Application Program Interface (API) specifies the details of how an application program interacts with protocol software. Although details depend on the operating system, the socket API is a de facto standard. A program creates a socket, and then in-vokes a series of functions to use the socket. A server using the stream paradigm calls socket functions: socket, bind, listen, accept, recv, send, and close; a client calls socket, connect, send, recv, and close.
Because many servers are concurrent, sockets are designed to work with concurrent applications. When a new thread is created, the new thread inherits access to all sockets that the creating thread owned.
EXERCISES
3.1 What are the two basic communication paradigms used in the Internet?
3.2 Give six characteristics of Internet stream communication.
3.3 Give six characteristics of Internet message communication.
3.4 If a sender uses the stream paradigm and always sends 1024 bytes at a time, what size blocks can the Internet deliver to a receiver?
3.5 If a sender wants to have copies of each data block being sent to three recipients, which paradigm should the sender choose?
3.6 What are the three surprising aspects of the Internet’s message delivery semantics?
3.7 Give the general algorithm that a connection-oriented system uses.
3.8 When two applications communicate over the Internet, which one is the server?
3.9 Compare and contrast a client and server application by summarizing characteristics of each.
3.10 What is the difference between a server and a server-class computer?
3.11 Can data flow from a client to a server? Explain.
3.12 List the possible combinations of clients and servers a given computer can run.
3.13 Can all computers run multiple services effectively? Why or why not?
3.14 What two identifiers are used to specify a particular server?
3.15 List the steps a client uses to contact a server after a user specifies a domain name for the server.
Exercises 47
3.16 What basic operating system feature does a concurrent server use to handle requests from multiple clients simultaneously?
3.17 What performance problem motivates peer-to-peer communication?
3.18 Name two operating systems that offer the socket API.
3.19 Once a socket is created, how does an application reference the socket?
3.20 What are the main functions in the socket API?
3.21 Give the typical sequence of socket calls used by a client and by a server.
3.22 To what socket functions do read and write correspond?
3.23 Does a client ever use bind? Explain.
3.24 Why is symbolic constant INADDR_ANY used?
3.25 Is sendto used with a stream or message paradigm?
3.26 Suppose a socket is open and a new thread is created. Will the new thread be able to use the socket?
3.27 Examine the web server in Appendix 1, and build an equivalent server using the socket API.
3.28 Implement the simplified API in Appendix 1 using socket functions.