HTTP and socket long connection and short connection difference

Source: Internet
Author: User
Tcp / ip
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: TCP includes FTP, HTTP, TELNET, SMTP and other protocols
UDP includes DNS, TFTP, and other protocols
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 addition of persistent connection support (which appears to be the latest http1.0 the specified keep-alive), but stateless or untrustworthy.

When to use long connection, short connection.
Long connection is often used for frequent operation, point-to-point communication, and the number of connections can not be too much. Each TCP connection requires a three-step handshake, which takes time, if each operation is the first connection, then the processing speed will be reduced a lot, so after each operation is constantly open, secondary processing directly send packets on OK, do not establish a TCP connection. For example: A database connection with a long connection, if the use of short connection frequent communication will cause socket errors, 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, the connections of thousands or even billions of clients, such as Web sites, are more resource-intensive, if connected with long, and thousands of users, Imagine if each user uses a connection. 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.
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 sent out after not infinite wait, need to set the timeout time, more than the 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.

What's the socket?

The socket is the intermediate software abstraction layer of the application layer communicating with the TCP/IP protocol family, which is a set of interfaces. In design mode, the socket is actually a façade mode, which is the complex TCP/IP protocol family hidden behind the socket interface, for users, a group of simple interface is all, let the socket to organize data to meet the specified protocol.


Communication process:

Host A's application should be able to communicate with host B's application, and must establish a connection through the socket, while establishing a socket connection must require the underlying TCP/IP protocol to establish a TCP connection. Establishing a TCP connection requires the underlying IP protocol to address the host in the network. We know that the IP protocol used by the network layer can help us locate the target host based on the IP address, but there may be multiple applications running on one host, and how to communicate with the specified application must be specified by TCP or UPD address, which is the port number. This allows a single Socket instance to represent the communication link of an application on one host.

Establish a communication link
When the client wants to communicate with the server, the client first creates a socket instance, the operating system assigns an unused local port number to the socket instance, and creates a socket data structure that contains both local and remote addresses and port numbers. This data structure will remain in the system until the connection is closed. Before the constructor that creates the Socket instance returns correctly, a three handshake protocol will be performed for TCP, and the socket instance object will be created when the TCP handshake is complete, otherwise a IOException error will be thrown.
The corresponding server will create a ServerSocket instance, ServerSocket creation is simpler as long as the specified port number is not occupied, the common instance creation succeeds and the operating system creates an underlying data structure for the ServerSocket instance. This data structure contains the port number that specifies the listener and the wildcard character that contains the listener address, which is usually "*" that listens to all addresses. Later, when the Accept () method is invoked, the blocking state is entered, waiting for the client's request. When a new request arrives, a new socket data structure is created for the connection, which contains the address and port information that is the source address and port of the request. This newly created data structure will be associated with a list of incomplete connection data structures for the ServerSocket instance, noting that the socket instance that corresponds to the server does not complete the creation, and that the socket instance on this server will not return until the three handshake with the client is completed. and move the data structure of this Socket instance to the completed list from the finished list. Therefore, each data structure in the associated list of ServerSocket represents a TCP connection with a client's establishment.

Note:
Maximum number of single computer TCP connections under Windows
Adjusting system parameters to adjust the maximum number of TCP connections for a single machine, the number of TCP connections for a single computer under Windows can be determined together by several parameters:
All of the following are by modifying the registry [HKEY_LOCAL_MACHINE \SYSTEM \CurrentControlSet \services \TCPIP \parameters]

1. Maximum number of TCP connections tcpnumconnections
2.TCP Shutdown delay Time TcpTimedWaitDelay (30-240) s
3. Maximum dynamic port number MaxUserPort (default = 5000, Max = 65534) TCP client and server connections, the client must allocate a dynamic port, by default the dynamic port is allocated 1024-5000, which means by default, The client can initiate a maximum of 3977 Socket connections at the same time
4. Maximum TCB quantity MaxFreeTcbs
Each TCP connection is assigned a TCP control block (the TCP controls block or TCB), which is used to cache some of the parameters of the TCP connection, and each TCB needs to allocate 0.5 KB Pagepool and 0.5KB Non-pagepool, which means Each TCP connection consumes 1KB of system memory.
Non-Server version, the default value for MaxFreeTcbs is 1000 (more than 64M of physical memory) Server version, which defaults to 2000. That is, by default, the Server version can establish and maintain up to 2000 TCP connections at the same time.
5. The maximum TCB hash table quantity MaxHashTableSize TCB is managed by hash table.
This value indicates the number of allocated pagepool memory, that is, if MaxFreeTcbs = 1000, then the amount of pagepool memory is 500KB so maxhashtablesize should be greater than 500. The larger the number, the higher the hash table's redundancy, and the less time it takes to allocate and find a TCP connection. This value must be a power of 2 and a maximum of 65536.

Typical configuration of IBM WebSphere Voice server under Windows Server 2003
MaxUserPort = 65534 (Decimal)
MaxHashTableSize = 65536 (Decimal)
MaxFreeTcbs = 16000 (Decimal)
Here we can see that maxhashtablesize is configured to be 4 times times larger than MaxFreeTcbs, which can greatly increase the speed at which TCP is built.

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.