Short connection in socket with long connection, Heartbeat pack sample detailed

Source: Internet
Author: User
Tags pack

Original address: http://blog.csdn.net/fireroll/article/details/9043221

Introduction to TCP Connections
When the TCP protocol is used in network communication, a connection must be established between the server and the client before the true read and write operation.
When the read and write operation is complete, the two sides no longer need this connection when they can release the connection,
The connection is established with a handshake of three times, and the release takes 4 shakes.
So each connection is built to require resource consumption and time consumption.
Classic three-time handshake schematic:


Classic four-time handshake close diagram:

One, long connection and short connection
Long connection: means that multiple packets can be sent continuously on a TCP connection,
In the case of TCP connection retention, if no packet is sent, both sides are required to send a detection packet to maintain the connection;
Generally need to do their own online maintenance.
Short connection: Refers to the communication both sides have the data interaction, establishes a TCP connection, after the data sends completes, then disconnects this TCP connection;
Common banks use short connections.
The advantage of this is that it is simpler to manage, connections that exist are useful connections, and no additional control is required.

For example, HTTP, just connect, request, shutdown, process time is short, the server if not received a request for a period of time to close the connection.
In fact, long connection is relative to the usual short connection, that is, long time to maintain the client and server connection state.

The operation process of long connection and short connection
The usual short connection procedure is:
Connection → data transfer → close connection;

And long connections are usually:
Connection → data transmission → stay connected (heartbeat) → data transmission → keep connected (heartbeat) → ... → Close connection;

This requires a long connection in the absence of data communications, timed to send packets (heartbeat) to maintain the connection state,
Short connections can be closed directly without data transfer.

When to use long connection, short connection.
Long connections are often used for frequent operation, point-to-point communication, and the number of connections cannot be too much.
Each TCP connection requires a three-step handshake, which takes time, and if each operation is connected first, then the processing speed is much lower.
So after each operation is kept open, the next time processing directly send packets on the OK, do not have to establish a TCP connection.

For example, a database is connected with a long connection,
If frequent communication with short connections causes socket errors, and frequent socket creation is also a waste of resources.

Second, send receive mode
1, asynchronous
Message sending and receiving are separate, independent, and do not affect each other. This approach is divided into two different situations:
(1) Asynchronous duplex: Receive and send in the same program, two different sub processes are responsible for sending and receiving
(2) Asynchronous Simplex: receiving and sending is done with two different programs.

2, synchronization
Message sending and receiving is synchronous, both message sending and waiting for receiving return message.
The synchronization method generally needs to consider the timeout problem, that is, the message can not wait indefinitely after sending out, need to set the timeout time,
More than this time the sender is no longer waiting to read the return message, direct notification timeout return.

In the long connection there is generally no condition to determine when reading and writing to end, so must be added to the length of the message header.
The reading function reads the length of the packet head, and then reads the corresponding length of the message according to the length.

Three. Single, half duplex and full duplex
According to the Division of communication between the two parties and signal transmission direction can be divided into three kinds of communication methods:
Simplex
Half Duplex,
Full-duplex

In the computer network mainly using duplex mode, which:
The LAN uses half duplex mode,
Metropolitan area Network and WAN adopt the whole double year way.

1. Simplex (Simplex) mode:
Both the transmitter and receiver in the communication equipment are clearly divided, and the data can only be transmitted in a single fixed direction from the transmitter to the receiver.
A typical sending device, such as an early computer card reader, that uses a single-work communication, typically a receiving device such as a printer.

2. Half-duplex (Half Duplex) mode:
The communication device is both a transmitter and a receiver, and the two devices can transmit data to each other, but only in one direction at a time.
For example, a walkie-talkie is a half-duplex device because only one side can speak at a time.

3. Full-duplex (full Duplex) mode:
The communication device is both a transmitter and a receiver, and two devices can transmit data in two directions at the same time.
For example, the phone is a full-duplex device because both sides can talk at the same time.

HTTP services like Web sites generally use short links, because long connections consume a certain amount of resources for the server,
And the tens of thousands or even billions of clients like Web sites that connect with a short connection will save some resources,
If you use a long connection, and there are thousands of users at the same time, if each user is occupied by a connection, it can be imagined.
Therefore, a large number of concurrent, but each user without frequent operation of the need for a short link good.

In short, the choice between long and short connections depends on the situation.

四、一个 simplest long connection and heartbeat retention sample program
/*!
******************************************************************************
* \file
*
* \brief
*
* \author
* Hank
******************************************************************************
*/
[CPP]View Plain copy print?  #include <stdio.h> #include <string.h> #include <errno.h> #include <sys/socket.h> #include <resolv.h> #include <stdlib.h> #include <netinet/in.h> #include <arpa/inet.h> #include &l t;arpa/inet.h> #include <unistd.h> #include <sys/time.h> #include <sys/types.h>
#define MAXBUF 1024


int main (int argc, char **argv)
{
int SOCKFD, Len;
struct sockaddr_in dest;
Char Buffer[maxbuf];
Char heartbeat[20] = "Hello server";
Fd_set RfDs;
struct Timeval TV;
int retval, maxfd =-1;

if (argc!= 3)
{
printf ("error! The right format should is: \
\n\t\t%s IP port\n\t eg:\t%s127.0.0.1 80\n ",
Argv[0], argv[0]);

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.