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.
At the application level: TCP includes FTP, HTTP, TELNET, SMTP, and other protocols
UDP includes DNS, TFTP, and other protocols
Short Connection
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 connection to 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 that it adds support for persistent connections (which looks like the latest http1.0 can display the specified keep-alive), but is stateless, or not trusted.
when to use long connections, short connections?
Long connections are used to operate frequently, point-to-point communications, and the number of connections can not be too many cases. Each TCP connection requires a three-step handshake, which takes time, if each operation is first connected, then operation then the processing speed will be reduced a lot, so after each operation is continuously open, the processing of the direct sending of packets is OK, do not establish a TCP connection. For example, the connection of the database with a long connection, if the frequent communication with short connections 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, and tens of thousands or even billions of clients, such as Web sites, use short connections to save resources if they are connected with long connections, and thousands of users at the same time, If each user occupies a connection, then you can imagine it. So the concurrency is large, but each user needs to be short-connected in case of frequent operation.
In summary, the choice of long connections and short connections is subject to availability.
Send receive 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, by two different sub-processes are 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 out, 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.
In a long connection, it is generally not possible to determine when the read-write ends, so it is necessary to add a length message header. The reading function reads the length of the message header, and then reads the corresponding length of the message according to the length.
What is a socket?
A socket is an intermediate software abstraction layer that the application layer communicates with the TCP/IP protocol family, which is a set of interfaces. In design mode, the socket is actually a façade mode, it is the complex TCP/IP protocol family hidden behind the socket interface, for the user, a set of simple interface is all, let the socket to organize data to meet the specified protocol.
Host A's application communicates with host B's application and must establish a connection through the socket, and the socket connection must be built with the underlying TCP/IP protocol to establish a TCP connection. Establishing a TCP connection requires the underlying IP protocol to address the hosts in the network. We know that the IP protocol used by the network layer can help us to 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 is specified by the TCP or UPD address, or the port number. This makes it possible for a Socket instance to uniquely represent a communication link for an application on a 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 the socket instance a local port number that is not used, and creates a socket data structure that contains local and remote addresses and port numbers. This data structure will remain in the system until the connection is closed. Before the constructor for the socket instance is returned correctly, the three-time handshake protocol for TCP will be made, and after the TCP handshake protocol completes, the socket instance object will be created, or 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 general instance creation will succeed, and the operating system will create an underlying data structure for the ServerSocket instance. This data structure contains the port number of the specified listener and the wildcard character that contains the listening address, usually "*" which listens to all addresses. Then when the accept () method is called, it goes into a blocking state and waits 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 for the requested source address and port. This newly created data structure will be associated to an incomplete list of connection data structures for the ServerSocket instance, and note that the server's corresponding socket instance is not created, and the socket instance of the server is not returned until the three handshake with the client is completed. and move the data structure of the Socket instance to the completed list in the never-complete list. Therefore, each data structure in the list associated with ServerSocket represents a TCP connection that is established with a client.
remark:
Maximum number of TCP connections for a single machine under Windows
Adjusting the system parameters to adjust the maximum number of TCP connections for a single machine, the number of TCP connections under Windows is determined by multiple parameters:
The following are all modified by the registry [HKEY_LOCAL_MACHINE \SYSTEM \CurrentControlSet \services \TCPIP \parameters]
1. Maximum number of TCP connectionsTcpNumConnections
2.TCP shutdown delay TimeTcpTimedWaitDelay (30-240) s
3. Maximum number of dynamic portsMaxUserPort (default =, Max = 65534) TCP client and server connections, the client must be assigned a dynamic port, by default, the dynamic port is allocated a range of 1024-5000, which means by default, Clients can initiate up to 3977 Socket connections at the same time
4. Maximum TCB quantityMaxFreeTcbs
The system assigns a TCP control block to each TCP connection (TCP-control block or TCB), which is used to cache some parameters of a TCP connection, and each TCB needs to allocate 0.5 KB of Pagepool and 0.5KB Non-pagepool, saying that Each TCP connection consumes 1KB of system memory.
For non-Server versions, the default value for MaxFreeTcbs is 64M or more of the server version of physical memory, which defaults to 2000. In other words, by default, the Server version can establish and maintain up to 2000 TCP connections at a time.
5. Maximum TCB Hash table numberThe MaxHashTableSize TCB is managed by a hash table.
This value indicates the amount of allocated pagepool memory, that is, if MaxFreeTcbs = 1000, the amount of memory Pagepool is 500KB then the maxhashtablesize should be greater than 500. The larger the number, the higher the redundancy of the hash table, and the less time it takes to allocate and find the TCP connection. This value must be a power of 2 and a maximum of 65536.
Typical configurations for 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 of TCP setup.
HTTP and socket long connections and short connection differences