Tcp/ip,http,socket, long connection, short connection--summary.

Source: Internet
Author: User
Tags socket error

Thomescai Http://blog.csdn.net/thomescai (reprint please keep)

Summary:

Before these concepts a little confused, look up some information, a little summary of their differences. If there is a mistake, please shoot ~ ~ ~

First look at the picture:


What TCP/IP is.

TCP/IP is a protocol group that can be divided into three tiers: the network layer, the transport layer, and the application layer.
There are IP protocols, ICMP protocols, ARP protocols, RARP protocols and BOOTP protocols on the network layer.
There are TCP protocols and UDP protocols in the transport layer.
In the application layer are FTP, HTTP, TELNET, SMTP, DNS and other protocols.

What's the socket?

Socket is the application layer and TCP/IP protocol Family Communication intermediate software abstraction layer, a set of interfaces, the complex TCP/IP protocol family hidden behind the socket interface.

so HTTP is the TCP/IP application layer protocol. the socket is their software abstraction layer.

Short Connection:
Connect-> Transmit data-> close connection
HTTP is stateless, and the browser and server make a connection once for each HTTP operation, but the connection is interrupted at the end of the task.
You can also say this: Short connection refers to the socket after the connection sent after receiving data immediately disconnect.
Long connection:
Connect-> transmit Data-> Keep connected-> transmit data-> ... -> closes the connection.
A long connection is a connection that is maintained after the socket connection is established, regardless of whether it is used, but is less secure.
Long connection to http:
HTTP can also establish long connections, using Connection:keep-alive,http 1.1 to default to persistent connections. The biggest difference between HTTP1.1 and HTTP1.0 is the increased support for persistent connections (appearance
As the latest http1.0 can show the specified keep-alive), it is still stateless, or cannot be trusted.


reference materials:

http://blog.csdn.net/xijiaohuangcao/article/details/6105623--"To those who make me confused http, TCP, UDP, Socket"

http://hi.baidu.com/loleafver/blog/item/a222c9d853d4f1e638012fa0.html--"HTTP long connection and short connection"

http://hi.baidu.com/einsof/blog/item/f2f3a84b5d1a30fb82025ca0.html--"What is short connection, long connection, Socket"

================================================

Socket

The image description of the socket agreement
The original meaning of the socket is "hole" or "socket". Here as a process communication mechanism for 4BDS Unix, take the latter meaning. The socket is very similar to a telephone socket. Take a national telephone network for example. Telephone calls are equivalent to each other 2 processes of communication, the area code is its network address, a unit in the area of the switch equivalent to a host, the host assigned to each user's internal number is equivalent to the socket number. Any user prior to the call, first to occupy a telephone, equivalent to the application of a socket, and to know the other person's number, equivalent to the other side has a fixed socket. Then dial the call to the other side, which is equivalent to sending a connection request (if the other person is not in the same zone, and dial the other area code, equivalent to the network address). If the other party is present and free (equivalent to another host of communication and can accept the connection request), pick up the phone microphone, the two sides can officially call, the equivalent of successful connection. Both sides of the call process, is a direction of the telephone signal and the other from the phone to receive signals, the equivalent of sending data to the socket and receive data from the socket. After the call is over, one side hangs up the telephone equivalent of closing the socket and undoing the connection.

See Encyclopedia for detailed content.


Usually a short connection is this: connection-> transmission data-> close connection
That what is the long connection. For a general long connection relative to a short connection, a long connection does not close the connection after the transmission is completed, while the continuous sending package remains connected pending processing of the next packet.
such as: Connect-> transmit Data-> Keep connection-> transmit data-> ... -> closes the connection.

When do you use a short connection?
General long connection for a few client-end to server-end frequent communication, such as: Database connection with long connection, if the use of short connection frequent communication will cause socket error, and frequent socket creation is also a waste of resources.
HTTP services like Web sites generally use short links, because long connections consume a certain amount of resources for the server, and the connections of tens of thousands or even billions of clients, such as Web sites, can save some resources. (View source)


TCP/IP Communication interpretation length link

One. Communication mode
There are three major categories:
A Server/client Way
1. A client side connects to a server side, or point-to-point (peer to peer):
2. Multiple client parties connect to a server side, which is also the usual concurrent server approach.
3. A client-side connection to multiple server parties is rare, mainly
For a client to send requests to multiple servers.

(ii) connection mode
1. Long connection
The client and the server first establish a communication connection, and the connection is established and continuously opened.
Then the message is sent and received. In this way because the communication connection has been
exists, you can use the following command to see if the connection is established:
Netstat–f inet|grep Port number (for example, 5678).
This approach is often used for point-to-point communication.

2. Short connection
Client side communicates with server every time a message is sent and received
Then disconnect immediately after the transaction is complete. This approach is often used in a little bit of
Communications, such as multiple client connections to a server.

(iii) Sending and receiving mode
1. Asynchronous
Message sending and receiving are separate, independent, and do not affect each other. Such side
The formula is divided into two types:
(1) Asynchronous duplex: Receive and send in the same program, there are two different
The child processes are responsible for sending and receiving, respectively
(2) Asynchronous Simplex: receiving and sending is done with two different programs.
2. Sync
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 be unlimited when sent up
To wait, you need to set the timeout time, more than the time the sender is no longer waiting to read the return report
text, direct notification timeout return.

The actual communication mode is the combination of these three kinds of communication methods. For example, the general book offers
TCP/IP sample programs are mostly synchronous short connection server/client programs. Some
The combination is basically unused, and the more commonly used valuable combinations are the following:

Synchronous Short Connection Server/client
Connecting Server/client with step size
Asynchronous Short Connection Server/client
Asynchronous Long Connection Duplex server/client
Asynchronous long connection single work server/client

Where asynchronous long connection duplex is the most complex form of communication, sometimes
There is often a communication between two sets of systems between different banks or different cities.
such as gold project. Because these kinds of communication methods are relatively fixed, so you can advance
The template program for these types of communication is developed first.

Two. Message format
The diversity of communication message format is more, so it is necessary to design corresponding read-write message
Collect and send a paper function.

(i) Blocking and non-blocking mode
1. Non-blocking mode
Read the function to continuously read the action, if no message received, waiting for a period of time after
Timeout, which typically requires specifying a time-out period.
2. Blocking mode
If no message is received, the read function waits until a message arrives.

(ii) Circular reading and writing mode
1. One time direct read and write message
To read or complete at once, without distinction, in a single receive or delivery text.
Send a report text section.
2. Do not specify the length of cyclic reading and writing
This generally occurs in the short connection process, subject to network routing restrictions, a longer report
The article may be decomposed into several packets during the network transmission. One read may not
To read a message all at once, this will require a circular reading until the reading is complete.

3. With length packet head cyclic reading and writing
This situation is typically in the long connection process because there is no condition in the long connection to
To determine when the cycle of reading and writing is over, you must add a length message header. Read function
First read the length of the packet head, and then according to the length of the newspaper. In practice,
The header's code format is also often different, if the non-ASCII message headers, but also must
Converted to ASCII, common message header codes are:
(1) n-byte ASCII code
(2) N-byte BCD code
(3) N-byte network integer code

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.