Chapter II Linux Network Programming
2.1 customer-Server model
At present, most Web applications are written with a client-server model, assuming that one end is a customer and the other is a server, so that the server provides the customer with a certain service content. It requires a party (server side) to wait for other client programs to communicate with it after starting the execution program (indefinitely). There are two specific types: and the hairstyle interacts with the repetitive type of interaction.
(1) and hairstyle interaction. In the interactive hairstyle mode, the main operating steps of the program are as follows:
• Wait for the arrival of a customer request;
• Generate a new process or task to process this customer request, and here you can also receive requests from other clients and terminate the process after processing is completed;
• Feedback client;
• Wait for new customer requests to arrive and proceed with the next service, so that the cycle operates.
(2) Repetitive interaction. In the repetitive interaction mode, the main operating steps of the program are as follows:
• Wait for the arrival of a customer request;
• Handle customer requests and services to customers;
• Feedback to customers and end of service;
• Wait for the next request to come, so loop.
The disadvantage of repetitive interactions here is that the server cannot receive and process requests from other clients while the servers are processing the request service to a client, and the hairstyle interaction solves this problem, but with the support of the multi-threading operating system. In general, UDP servers use repetitive interactions, and the TCP server uses and hairstyles to interact. Client/server mode requires that each application be composed of two parts: one section is responsible for initiating the communication and the other is responsible for answering it. They are typically run on different hosts, called Clients and servers, respectively. A server is not specific to a machine on a network, it can be any program that can serve on the network, and the client is also the application that the user needs to run in order to get a service. Due to the different functions of the client and server in network communication, different algorithms are used to program them.
2.2 for connected and disconnected clients-servers
When designing client-server software, you must choose between two types of interactions: a no-connect style or a connection-oriented style. The interaction of these two styles directly corresponds to the two main transport protocols provided by the TCP/IP protocol family. If the client and server communicate using User Datagram (UDP), then the interaction is not connected, and if Transmission Control Protocol (TCP) is used, the interaction is connection-oriented. The client-server model with the TCP protocol is our first choice because the connection-oriented interaction protocol makes programming easier and the program more reliable.
2.3 TCP connecting the communication process
TCP is a connection-oriented, reliable byte-stream transmission protocol, before communication, the two parties must establish a connection between the end of the communication and then release.
In order to establish a reliable connection, TCP uses a three-time handshake method:
(1) The client (requester side) sends a synchronization segment (SYN) to the server, requesting access.
(2) The server issues a sync-answer segment (Syn-ack) to the client. On the one hand as a response to customer request access, on the other hand requires the client also access.
(3) The client sends a reply segment (ACK) to the server. As a response to a request for access to the server.
Terminating a TCP connection is a total of 4 handshakes:
(1) The client sends a shutdown segment (FIN) to the server. At this point, the client is no longer able to send data to a remote server, but can continue to receive data.
(2) The server issues a shutdown-reply segment to the client. At this point the server can also send data to the client, that is, the access is in a "semi-closed" state.
(3) The server sends a closing segment (FIN) to the client, closes the access on the side, and can still receive data. On the one hand as the customer closes the access response, on the other hand requires the client must also shut down access. At this point, the server can no longer send data.
(4) The client sends a shutdown-answer segment to the server in response to a server shutdown.
2.4 TCP socket programming for Protocols
Socket programming is simply to make two or more networked computers exchanging data with one another.
2.4.1 TCP The invocation of the primary function in the socket interface connection
The function of the main call in the TCP socket interface connection is socket,bind,connect,write,listen,close, and so on. The client/server mechanism of the TCP/IP protocol works as shown in:
2.4.2 Linux data structure of the socket interface
The data structure of a set interface is related to the network that uses it. In Linux, each protocol has its own network address data structure that begins with sockaddr_, with different suffixes representing different protocols, such as IPv4 corresponding to sockaddr_in.
2.5 Programming Algorithm Implementation
2.5.1 client-side algorithm
The client can construct a connection to a server and communicate with the server according to the following algorithm.
(1) Locate the IP address and protocol port number of the server with which it communicates;
(2) allocation of sockets;
(3) indicates that this connection requires an arbitrary, unused protocol port in the local machine and allows TCP to select one of these ports;
(4) Connect the socket to the server;
(5) Communicating with the server using the application-level protocol (this often includes sending requests and waiting for an answer);
(6) Close the connection.
2.5.2 the algorithm of the service side
(1) Concurrent Server
A server that can handle multiple requests at a time is called a concurrent server. We use concurrent, connection-oriented server-based algorithms, and the following are the steps that the concurrent server uses for connection-oriented protocols:
Main 1. Create the socket and bind it to the set address of the provided service. The socket remains
Non-connected.
Master 2. Set the port to passive mode so that it is ready for use by the server.
Main 3. Call accept repeatedly to receive the next connection request from the customer and create a new
Thread or process to handle the response.
From 1. The connection request is passed by the main thread (that is, the socket for the connection begins).
From 2. Use this connection to interact with the customer: Read request and response back.
From 3. Close the connection and exit. Exits from the thread after all requests from the customer have been processed.
(2) multi-threaded and multi-process concurrent servers
Linux provides two forms of concurrency--processes and threads, so there are two common main-slave pattern implementations: one is that the server creates multiple processes, and each process has one thread of execution. Another implementation is that the server creates multiple threads of execution in a process.
(3) relies on single-threaded and asynchronous I/O servers that handle multiple connections
in a single-threaded implementation, one thread of execution manages multiple connections. It achieves surface concurrency by using asynchronous I/O. The server repeatedly waits for I/O on the connection it is opening, and receives the request for processing. Because a single thread handles all connections, it can share data across multiple connections. However, because the server has only one thread, it does not process requests faster than the loop server, even on a computer with multiple processors. The application must share data or have a short processing time for each request, as long as such a server implementation scenario is desirable in this case.
Linux Network programming