为什么我发烧了 鼻子鼻梁会肿的 会疼呢 那是怎么回事
CP3003 Tutorial Week 3 – Solutions
It is a relationship between two computer hosts in which one host takes on the role of requesting services and the other the role of responding to service request.
The server starts life with a single thread which we will call the main thread. This thread creates a socket, binds an address, sets the connection queue length then sits in an infinite loop accepting connection requests. Each time a client connects, the operating system creates a new socket for the new connection. The main thread returns from the accept system call and a newly created socket is returned. The main thread creates a new thread to handle the new connection. In a multithreaded environment, the new thread operates concurrently with the main thread. While the new thread is handling the connection, the main thread continues in its loop accepting new connections and creating a new thread to service each new connection. If all threads run concurrently, a single server process can be handling multiple client connections concurrently.
Consider the action of maintaining a count of the total number of clients serviced or of creating a unique, monotonically increasing sequence number. If two client threads both try to increment an integer at the same time, the result may be unpredictable: both read the value (N, say) into a register, both increment the value in the register (le***ing N+1) and both store the incremted value into memory. The result is N+1 instead of the desired N+2.
N + 1, one main thread and N threads for processing those N clients.
IP address, port number, protocol
Recall that, while UDP and TCP are both transport protocols, they h***e different semantics. TCP is connection-oriented and provides a reliable byte stream; UDP is connectionless and does not guarantee in-order delivery or even delivery at all. A consequence of the connection aspect is that it takes time to set up a TCP connection and time to tear it down.
Consider a server that has two modes of interaction. One mode is a simple request-response interaction (such as getting the time of day). The amount of data that is transferred is minimal; it fits in one packet. Setting up and tearing down a TCP connection for this type of quick interaction would more than double the time required and the benefit would be in-order delivery of a single packet. It makes sense to use UDP for this style of interaction.
Conversely, an interaction that requires transfer of large amounts of data would benefit from TCP’s sliding window. The window mechanism makes the transfer so much more efficient that it is well worth the overhead of an initial connection setup and a final connection tear down.
The TCP protocol layer receives segments from the IP layer below. When a segment arrives, the TCP layer has to identify the application (regardless of whether it is the client or the server) to which the segment is addressed. Since TCP uses the port number to identify the target application, if the application hasn’t registered a port number with TCP, then TCP will be unable to deliver the segment. Consequently, TCP requires that both servers and clients register a port number with it before any segments are sent.
The server port number gets registered when the server is created, the port number to be used is specified explictly.
The client port number is usually assigned by system (at random, not in use, and other than special port number).
ServerSocket is created to wait and listen on a specific port. When request from client arrive, the ServerSocket wakes up and creates another Socket for handling the request and performing read/write.
ServerSocket. If no client request arrives, accept() blocks. When request arrives, it returns a Socket object that connects the client with the server.
The client request is rejected because the queue is full. If queue length is not specified, it is determined by system.
SeverSocket() creates an unbound server socket, you need to call method bind() to bind it to a specific port. When IP address is supplied, the server socket is created for that address. A host can h***e many network interfaces, ServerSocket(port,qlen) creates server sockets for every network interfaces, but ServerSocket(port,qlen.addr) creates socket for that interface only.