Persistent connections and short connections)

Source: Internet
Author: User

Persistent connectionAndTransient connection

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

There are three main communication methods::

(I) Server/clientMethod

1.OneClientConnect oneServerParty, or point-to-point(Peer to Peer):

2.MultipleClientConnect oneServerThis is also the common concurrent server mode.

( 2 ) connection mode

1. persistent connections client party and server party first establishes a communication connection. After the connection is established, it continues to open and then sends and receives 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 ( example 5678) . This method is often used for point-to-point communication.

2.Transient connectionClientFang heServerCommunication is performed every time a message is sent and received, and the connection is closed immediately after the transaction is completed. This method is often used for one-to-multiple-point communication, such as multipleClientConnect oneServer.

(3.)Sending and receiving Method

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

(1)Asynchronous duplex: receives and sends messages in the sameProgramTwo different sub-processes are responsible for sending and receiving

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

2.Synchronous message transmission and receiving are both synchronous, that is, the packet is waiting to receive 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/IPMost of the sample programs are synchronized with short connections.Server/clientProgram.

Some combinations are basically unnecessary. The most common and valuable combinations are the following:

Synchronous short connectionServer/client

Same step connectionServer/client

Asynchronous short connectionServer/client

Asynchronous persistent connection duplexServer/client

Asynchronous persistent connection ticketServer/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.

II.Message formats: communication messages have more diverse formats. Therefore, the function for receiving and sending read/write packets must be designed accordingly.

(I)Blocking and non-blocking Modes

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 generally need to specify the timeout time.

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

(II)Cyclic read/write mode

1.One direct read/write Packet

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

2.Loop 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.ASCIIThe packet header must also be convertedASCII,Common packet header code systems include:

(1) NByteASCIICode

(2) NByteBCDCode

(3) NNetwork integer in bytes

The above are several typical read/write packet methods. You can provide some typical APIs together with the communication mode template.Read/write functions. Of course, in actual problems, it may be necessary to write the corresponding read/write protocol to the recipient's message format.API.

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.