TCP persistent connection and short connection

Source: Internet
Author: User

People who are new to TCP/IP communication design can quickly compile a communication procedure based on the example. Some may think that TCP/IP programming is very simple. In fact, TCP/IP programming is rich. The richness of programming mainly lies in the diversity of communication methods and message formats.

I. 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.
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. After the connection is established, the connection is continuously enabled, and then the packets are sent and received. In this way, because the communication connection persists, you can run the following command to check whether the connection is established:
Netstat-F Inet | grep port number (for example, 5678 ).

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 immediately disconnected. 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 is divided into two situations:
(1) asynchronous duplex: receive and send in the sameProgramAt the same end, two different 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. After this time, the sender no longer waits to read the returned message and directly notifies the sender of the 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 it usually occurs between two systems in different banks or different cities.
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 format
Communication message formats are more diverse. Therefore, the function for receiving, receiving, and sending packets of the corresponding read/write packets must be designed accordingly.
(1) blocking and non-blocking methods
1. Non-Blocking Mode
The READ function does not stop reading. If no message is received, it will wait for a period of time before returning. In this case, you generally 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
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 transient connection. Due to network routing and other restrictions, a long packet may be divided into several packets 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 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. Read Functions
First, read the length of the packet header and then read 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 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 messages. You can provide some typical API read/write functions together with the communication mode template. Of course, in actual problems, you may also need to write the read/write API that matches the recipient's message format. in actual situations, we often need to connect our own system with others' systems. With the above templates and APIs, we can say that there is no problem in 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.