Persistent connection and short connection

Source: Internet
Author: User

Persistent connectionAndTransient connection

TCP/IP programming has rich content. The richness of programming mainly lies in the diversity of communication methods and message formats.

There are three main communication methods:

(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.

(2) Connection Method

1. The persistent Connection Client establishes a communication connection with the server. After the connection is established, the client continues to enable the connection before sending and receiving packets. In this mode, because the communication connection persists, run the following command to check whether the connection is established: netstat-F Inet | grep port number (such as 5678 ). This method is often used for point-to-point communication.

2. The short connection client and server communicate with each other only when sending and receiving packets. The connection is closed immediately after the transaction is completed. This method is often used for one-to-multiple-point communication. For example, multiple clients connect to one server.

(3) sending and receiving Methods

1. asynchronous messages are sent and received separately and independent from each other. This method can be divided into two situations:

(1) asynchronous duplex: receive and send in the sameProgramTwo different sub-processes are responsible for sending and receiving

(2) asynchronous Ticket: two different programs are used for receiving and sending.

2. synchronous message sending and receiving are both synchronous, that is, the packet is waiting for receiving the returned packet after being sent. The timeout issue needs to be considered in the synchronization mode, that is, the sender does not have to wait infinitely after the message is sent, and the time-out period needs to be set.,Return message, directly notifying timeout.

The actual communication mode is a combination of the three communication modes. For example, the TCP/IP sample programs provided in the general book are mostly server/client programs that synchronize short connections.

Some 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, asynchronous persistent connection duplex is the most complex communication method, and sometimes communication between two systems in different banks or cities often occurs. For example, the gold card project. Since these communication methods are relatively fixed, you can prepare template programs for these communication methods first.

2. Message formats communication message formats are more diverse. Therefore, the function for receiving and sending read/write packets must be designed accordingly.

(1) blocking and non-blocking methods

1. The non-blocking READ function does not stop reading. If no message is received, wait for a period of time and then return timeout. In this case, you need to specify the timeout time.

2. If no message is received in the blocking mode, the READ function remains in the waiting state until a message arrives.

(2) Cyclic read/write

1. One direct read/write Packet

All or all sent bytes are read at one time without adding them.

2. Circular read/write with no Length Specified

This usually occurs in short connection. Due to network routing and other restrictions, a long packet may be divided into several packets during network transmission. One read may not be able to 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 the persistent connection process. Because there is no condition in the persistent connection to determine when the cyclic read/write ends, the length of the packet header must be added. The READ function first reads the length of the packet header and then reads the packet according to the length. in actual situations, the Header Format is often different. If it is a non-ASCII packet header, it must also be converted to ASCII,Common packet header code systems 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 messages. You can provide some typical API read/write functions in advance with the communication mode template. Of course, in practice, you may have to write a read/write API that matches the recipient's message format..

Original article:

Http://www.cnblogs.com/liuyong/archive/2011/07/01/2095487.html

Http://wenku.baidu.com/view/c45a1926482fb4daa58d4b00.html

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.