Advanced programming python tutorial network connection -tcp long and short connection

Advanced programming python tutorial network connection -tcp long and short connection

tcp long connections and short connection

TCP before the actual read and write operations, it must establish a connection between the server and client,

When the write operation is completed, the two sides are no longer needed to connect the connection can be released,

Through the three-way handshake to establish a connection, you need to release the four-way handshake,

So is the need to establish a connection for each resource consumption and time consumption.

The whole process of the TCP communication, as shown below:

1. TCP short connection

Simulation case of a short by TCP connections:

client sends a connection request to the server

server receives the request, the parties to establish a connection

client sends a message to the server

server response to client

A complete reading and writing, any time the two sides can initiate a close operation

In a first step 5, the first client initiates usually close operation. Of course, we do not rule out special circumstances.

See from the above description, transmitting a short connection usually only read and write operations between client / server!

2. TCP long connection

Then simulate a long-connections:

client to the server to initiate a connection server receives the request, the two sides establish a connection client sends a message to server server to respond to client once write is complete, the connection is not closed subsequent read and write operations ... after a long operation client sends a request to close

3. TCP long / short connection operation

3.1 Procedure short connection are:

Establishing a connection - Data Transfer - Close ... connected to establish a connection - Data Transfer - close the connection

 

 

Procedure connection length is 3.2:

 

Establishing a connection - Data Transfer ... (stay connected) ... Data Transfer - close the connection

 

 

4. TCP long / short connection of the advantages and disadvantages

Long established TCP connection can save more and closed operations, reduce waste and save time.

For customers frequently requested resources, it is more suitable for long connection.

The connection between the client and the server if has not closed the case, there will be a problem,

With more and more client connections, server could not carry sooner or later when the server side this time need to take some strategy,

Some did not close the connection as long to read and write events, to avoid malicious damage connection lead to server-side services;

If the conditions can then allow a client machine as the particle size, limiting the maximum length of the number of connections per client,

This can completely avoid a pain egg hurt the back-end client service.

Short connections for the server management simpler, there are useful links are connected, no additional control.

However, if the customer requests frequently, will waste time and bandwidth on the establishment of TCP and closing operations.

5. TCP scenarios long / short connection

Long connections used for frequent operation, point to point communications, and the number of connections can not be too often.

Each TCP connection requires three-way handshake, it will take time, if each operation is first connected,

Then operate if the processing speed will be much lower, so do not disconnect after each operation,

Send packets directly on OK when the process again, do not establish a TCP connection

For example: the database is connected with a long connection, if the connection frequent cause short socket communication error,

And frequently socket creation is a waste of resources.

And like WEB site http services are generally short link because a long connection to the server, it will consume some resources,

And like WEB site so often thousands or even millions of clients connected by a short connection will save some more resources,

If you connect with a long, and at the same time there are thousands of users, if each user occupies one connection, then,

It can be imagined now. So concurrent capacity, but each user does not need to use the case of short frequent operation even better.

Guess you like

Origin www.cnblogs.com/gtea/p/12628619.html