What is persistent connection and what is short connection?

Source: Internet
Author: User

What is persistent connection and what is short connection?

Post a classic post. After reading it, there should be no problem:

Diversity of TCP/IP communication program design

People who are new to TCP/IP communication design can quickly compile a communication program based on the example.
According to this, some people may think that TCP/IP programming is very simple. Otherwise,
TCP/IP programming has rich content. The richness of programming is mainly reflected in
The diversity of communication methods and message formats.

I. Communication Method
There are three main categories:
(1) server/client Mode
1. A client connects to a server, or peer to peer ):
2. Multiple Clients connect to one server, which is also the common mode of concurrent server.
3. A client connects to multiple servers. This method is rare, mainly
It is used when a customer sends requests to multiple servers.

(2) Connection Method
1. persistent connection
The client establishes a communication connection with the server, and the connection is established continuously,
Then send and receive packets. In this mode, the communication connection is always
Yes. Run the following command to check whether the connection is established:
Netstat-F Inet | grep port number (for example, 567.
This method is often used for point-to-point communication.

2. transient connection
The client and server communicate and connect each time they send and receive packets.
After the transaction is completed, the connection is closed immediately. This method is usually used for one-to-multiple-point operations.
Communication, such as connecting multiple clients to a server.

(3) sending and receiving Methods
1. asynchronous
Messages are sent and received separately and independent from each other. This method
There are two situations:
(1) asynchronous duplex: There are two differences between receiving and sending in the same program.
Sub-processes are responsible for sending and receiving
(2) asynchronous Ticket: two different programs are used for receiving and sending.
2. Synchronization
Message sending and receiving are both synchronous, waiting for receiving the returned message after the message is sent.
The timeout issue needs to be considered in the synchronization mode, that is, the message cannot be infinite after it is sent.
Wait, you need to set the timeout time. The sender no longer waits for the read response
Message.

The actual communication mode is a combination of the three communication modes. For example
The TCP/IP sample programs are mostly server/client programs that synchronize transient connections. Yes
Combinations are basically unnecessary. The most common and valuable combinations are the following:

Synchronous transient connection server/client
Connect to server/client at the same step
Asynchronous short connection server/client
Asynchronous persistent connection duplex server/client
Asynchronous persistent connection Ticket Server/client

Among them, the asynchronous persistent connection duplex is the most complex communication method.
Communication usually occurs between two systems in different banks or cities.
For example, the gold card project. Because these communication methods are relatively fixed, you can pre-
First, compile the template programs for these communication methods.

Ii. Message format
Communication message formats are more diverse, and the corresponding read/write packets must be designed accordingly.
Function for receiving and sending packets.

(1) blocking and non-blocking methods
1. Non-Blocking Mode
The READ function keeps reading data. If no message is received, wait for a while.
Timeout is returned. In this case, you need to specify the timeout time.
2. Blocking Mode
If no message is received, the READ function remains in the waiting state until a message arrives.

(2) Cyclic read/write
1. One direct read/write Packet
Read or send all the messages separately at a time.
The byte of the sent message.
2. Circular read/write with no Length Specified
This usually occurs in short connection processes. Due to network routing restrictions, a long report is reported.
This article may be divided into several packages during network transmission. One read may not
You can read all the packets once. This requires reading the packets cyclically until the packets are read.

3. Loop read/write with length packet header
This is generally the case in a persistent connection process.
Determine when cyclic read/write ends, so the length of the packet header must be added. Read Functions
First, read the length of the packet header, and then read the packet according to the length. In actual conditions,
The format of the header is often different. If it is a non-ASCII packet header
Convert to ASCII. Common packet header code formats include:
(1) ASCII code of n Bytes
(2) n Bytes of BCD code
(3) network integer code of n Bytes

The above are several typical read/write packet methods that can be used together with the communication mode template.
Provides typical API read/write functions in advance. Of course, in actual problems
You must write a read/write API that matches the recipient's message format.

In actual situations, we often need to connect our own systems with others' systems,
With the above templates and APIs, it can be said that there is no problem connecting communication programs in any way.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.