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

Source: Internet
Author: User
Tags socket error

Profile:

Before these concepts a little confused, look up some information, a little generalization of their differences. If there is an error, please clap ~ ~ ~

First look at the picture:



What is TCP/IP?

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


What is a socket?

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

So HTTP is the protocol for the TCP/IP application layer. Sockets are their software abstraction layer.

Short connections:
Connection, transfer data, close connection
HTTP is stateless, and the browser and server make a connection every time an HTTP operation is made, but the end of the task interrupts the connection.
It is also possible to say that a short connection is when the socket connection is sent and the data is disconnected immediately after it is received.
Long connections:
Connect, transfer data--keep connected, transfer data ... , close the connection.
A long connection is when a socket connection is established, regardless of whether it is used or not, but is less secure.
Long connections for http:
HTTP can also be established for long connections, using Connection:keep-alive,http 1.1 to make persistent connections by default. The biggest difference between HTTP1.1 and HTTP1.0 is the increased support for persistent connections (appearance
Like the newest http1.0 can display the specified keep-alive), but still stateless, or can not be trusted.



Resources:

http://blog.csdn.net/xijiaohuangcao/article/details/6105623--to those who let 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 a short connection, a long connection, a Socket"

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

Socket

Image description of Socket protocol
The English literal of the socket is "hole" or "socket". Here as a 4BDS UNIX process communication mechanism, take the latter meaning. The socket is very similar to a telephone socket. Take a national telephone network as an example. Telephone calls on both sides of the equivalent of communication between the 2 processes, the area code is its network address, a unit in the zone of the switch equivalent to a host, the host assigned to each user's internal number equivalent to the socket number. Any user before the call, the first to occupy a telephone, equivalent to apply for a socket, and to know the other person's number, the equivalent of the other side has a fixed socket. Then dial the call to the other side, the equivalent of making a connection request (if the other party is not in the same area, also dial the other area code, equivalent to the network address). If the other party is present and idle (equivalent to the communication of another host to boot and can accept the connection request), pick up the telephone handset, the two sides can formally call, the equivalent of a successful connection. The process of the two sides of the call is the direction of the telephone signal and the other side from the telephone to receive the signal, the equivalent of sending data to the socket and receiving data from the socket. After the call is over, one side hangs up the phone equivalent to closing the socket and undoing the connection.

See the encyclopedia for more information.


This is usually the case with short connections: Connect, transfer data, close the connection
So what is a long connection? Generally long connections are relative to short connections, long connections do not close the connection after the number of transfers, and the constant sending packet keeps the connection waiting for the next packet to be processed.
such as: transmission data--transmission data--... , close the connection.

When do you use short connections?
The general long connection is used for the frequent communication of a few client-end to Server-end, for example: the connection of the database with long connection, if the communication with the short connection frequently causes the socket error, and the frequent socket creation is also a waste of resources.
HTTP services like Web sites typically use short links, because long connections can be resource-intensive for the server, and tens of thousands or even billions of clients, such as Web sites, with short connections can save resources. (View source)


TCP/IP communication interpreting short and long links

One. Communication mode
There are three major categories:
A Server/client Way
1. A client connects to a server party, or peer to peer:
2. Multiple clients connect to one server side, which is also the usual way of concurrent servers.
3. It is rare for a client to connect to multiple server parties, mainly
Used by one customer to send requests to multiple servers.

(b) Connection method
1. Long connection
The client and the server first establish a communication connection, after the connection is established,
The message is then sent and received. 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 (e.g. 5678).
This approach is often used for point-to-point communication.

2. Short connection
The client side communicates with the server every time the message is sent and received, and disconnects immediately after the transaction is completed. This approach is commonly used in a bit-to-multipoint communication, such as multiple client connections to a server.

(c) Sending and receiving mode
1. Asynchronous
Message sending and receiving are separate, independent and mutually exclusive. This approach is divided into two different situations:
(1) Asynchronous duplex: Receive and send in the same program, there are two different sub-processes responsible for sending and receiving respectively
(2) Asynchronous Simplex: Receive and send is done with two different programs.
2. Synchronization
The message is sent and received synchronously, which waits to receive the return message after the message is sent. Synchronous mode generally need to consider the time-out problem, that is, the message can not wait indefinitely after sending, need to set the timeout period, more than the time the sender no longer wait for the read return message, the direct notification timeout return.

The actual mode of communication is a combination of these three types of communication methods. For example, the general book provides the TCP/IP example program is mostly synchronous short-link server/client program. Some of the combinations are basic, the more common valuable combinations are the following:

Synchronous Short Connection Server/client
Same Step connection Server/client
Asynchronous Short Connection Server/client
Asynchronous long-connected duplex server/client
Asynchronous long-connected single-work server/client

Asynchronous long-connected duplex is the most complex form of communication, and sometimes it often occurs between two sets of systems between different banks or different cities. such as Gold Card project. Due to these kinds of communication methods are fixed, so you can pre-compile these kinds of Communication mode template program.

Two. Message format
Communication message format is more diverse, corresponding to the need to design the corresponding read and write message receiving and sending a paper function.

(i) Blocking and non-blocking modes
1. Non-blocking mode
The read function reads continuously, and if no message is received and a timeout is returned after a period of time, it is generally necessary to specify a time-out.
2. Blocking mode
If no message is received, the read function waits until a message arrives.

(b) Circular reading and writing mode
1. A direct read and write message
In a single receive or send a newspaper text action in one time without adding the full read or all the report text section.
2. Do not specify the length of the loop read/write
This generally occurs in a short connection process, limited by network routing, a longer message may be broken down into several packets during the network transmission. One read may not be able to read the message all at once, which requires a circular reading until it is finished.

3. Loop reading and writing with length message head
This is generally the case in the long connection process, because there is no condition in the long connection to determine when the loop read and write end, it is necessary to add the length of the message header. The reading function reads the length of the message header, and then goes to the reading paper according to that length. In fact, the code format of the header is often different, if the message header is non-ASCII, it must also be converted to ASCII, common message header code system has:
(1) n bytes of ASCII code
(2) N-byte BCD code
(3) N-byte network integer code

Tcp/ip,http,socket, long connection, short connection--summary (Turn)

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.