TCP/IP, http, socket, long connection, short connection

TCP/IP TCP/IP is a protocol group that can be divided into three layers: network layer, transport layer and application layer. In the network layer, there are IP protocol, ICMP protocol, ARP protocol, RARP protocol and BOOTP protocol. There are TCP and UDP protocols in the transport layer. In the application layer, there are: TCP including FTP, HTTP, TELNET, SMTP and other protocols                   UDP including DNS, TFTP and other protocols  Short connection connection -> transfer data -> close connection  HTTP is stateless, every time the browser and server perform an HTTP operation, The connection is established once, but the connection is terminated when the task ends. It can also be said that a short connection means that the SOCKET is connected and disconnected immediately after receiving the data after sending. Long connection connection -> transfer data -> keep connected -> transfer data -> . . . ->Close the connection. A long connection means that the connection is maintained regardless of whether it is used or not after the SOCKET connection is established, but the security is poor. HTTP long connection HTTP can also establish long connection, use Connection:keep-alive, HTTP 1.1 defaults to persistent connection. Compared with HTTP1.0, the biggest difference between HTTP1.1 and HTTP1.0 is the addition of persistent connection support (it seems that the latest http1.0 can display the specified keep-alive), but it is still stateless, or can not be trusted . When to use long connection, short connection? 





 



  
 


  
 

  
 
 Long connections are mostly used for frequent operations, point-to-point communication, and the number of connections cannot be too many. Each TCP connection requires a three-step handshake, which takes time. If each operation is connected first and then operated, the processing speed will be greatly reduced, so each operation will not be disconnected after each operation, and data packets will be sent directly during the next processing. OK, no need to establish a TCP connection. For example, long connections are used for database connections. If short connections are used for frequent communication, socket errors will occur, and frequent socket creation is also a waste of resources. And http services like WEB sites generally use short links, because long connections will consume a certain amount of resources for the server, and short links will be more economical for the connection of thousands or even hundreds of millions of clients as frequently as WEB sites. Some resources, if you use a long connection, and there are thousands of users at the same time, if each user occupies a connection, it can be imagined. Therefore, the amount of concurrency is large, but it is better to use a short connection if each user does not need to operate frequently. In short, the choice of long and short connections depends on the situation. Sending and receiving mode 1. Asynchronous message sending and receiving are separate, independent of each other and do not affect each other. This method is divided into two cases:  (1) Asynchronous duplex: receiving and sending are in the same program, and two different sub-processes are responsible for sending and receiving respectively  (2) Asynchronous simplex: receiving and sending are performed using two different procedures to complete. 2. The sending and receiving of synchronous messages are performed synchronously, that is, after the message is sent, it waits for the return message to be received. The synchronization method generally needs to consider the timeout problem, that is, after the message is sent, it cannot wait indefinitely, and a timeout period needs to be set. After this time, the sender no longer waits for the read return message, and directly informs the timeout to return. In a long connection, there is generally no condition to judge when the read and write ends, so the length header must be added. The read function first reads the length of the message header, and then reads the message of the corresponding length according to this length. what is socket
  

  

  
 
 



 

  


 

Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口。在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面,对用户来说,一组简单的接口就是全部,让Socket去组织数据,以符合指定的协议。

Socket 通信示例


主机 A 的应用程序要能和主机 B 的应用程序通信,必须通过 Socket 建立连接,而建立 Socket 连接必须需要底层 TCP/IP 协议来建立 TCP 连接。建立 TCP 连接需要底层 IP 协议来寻址网络中的主机。我们知道网络层使用的 IP 协议可以帮助我们根据 IP 地址来找到目标主机,但是一台主机上可能运行着多个应用程序,如何才能与指定的应用程序通信就要通过 TCP 或 UPD 的地址也就是端口号来指定。这样就可以通过一个 Socket 实例唯一代表一个主机上的一个应用程序的通信链路了。

建立通信链路
当客户端要与服务端通信,客户端首先要创建一个 Socket 实例,操作系统将为这个 Socket 实例分配一个没有被使用的本地端口号,并创建一个包含本地和远程地址和端口号的套接字数据结构,这个数据结构将一直保存在系统中直到这个连接关闭。在创建 Socket 实例的构造函数正确返回之前,将要进行 TCP 的三次握手协议,TCP 握手协议完成后,Socket 实例对象将创建完成,否则将抛出 IOException 错误。
与之对应的服务端将创建一个 ServerSocket 实例,ServerSocket 创建比较简单只要指定的端口号没有被占用,一般实例创建都会成功,同时操作系统也会为 ServerSocket 实例创建一个底层数据结构,这个数据结构中包含指定监听的端口号和包含监听地址的通配符,通常情况下都是“*”即监听所有地址。之后当调用 accept() 方法时,将进入阻塞状态,等待客户端的请求。当一个新的请求到来时,将为这个连接创建一个新的套接字数据结构,该套接字数据的信息包含的地址和端口信息正是请求源地址和端口。这个新创建的数据结构将会关联到 ServerSocket 实例的一个未完成的连接数据结构列表中,注意这时服务端与之对应的 Socket 实例并没有完成创建,而要等到与客户端的三次握手完成后,这个服务端的 Socket 实例才会返回,并将这个 Socket 实例对应的数据结构从未完成列表中移到已完成列表中。所以 ServerSocket 所关联的列表中每个数据结构,都代表与一个客户端的建立的 TCP 连接。
 
备注:
Windows 下单机最大TCP连接数
调整系统参数来调整单机的最大TCP连接数,Windows 下单机的TCP连接数有多个参数共同决定:
以下都是通过修改注册表[HKEY_LOCAL_MACHINE \System \CurrentControlSet \Services \Tcpip \Parameters]
 
1.最大TCP连接数      TcpNumConnections
2.TCP关闭延迟时间    TCPTimedWaitDelay    (30-240)s
3.最大动态端口数   MaxUserPort  (Default = 5000, Max = 65534) TCP客户端和服务器连接时,客户端必须分配一个动态端口,默认情况下这个动态端口的分配范围为 1024-5000 ,也就是说默认情况下,客户端最多可以同时发起3977 Socket 连接
4.最大TCB 数量   MaxFreeTcbs
系统为每个TCP 连接分配一个TCP 控制块(TCP control block or TCB),这个控制块用于缓存TCP连接的一些参数,每个TCB需要分配 0.5 KB的pagepool 和 0.5KB 的Non-pagepool,也就说,每个TCP连接会占用 1KB 的系统内存。
非Server版本,MaxFreeTcbs 的默认值为1000 (64M 以上物理内存)Server 版本,这个的默认值为 2000。也就是说,默认情况下,Server 版本最多同时可以建立并保持2000个TCP 连接。
5. 最大TCB Hash table 数量   MaxHashTableSize TCB 是通过Hash table 来管理的。
这个值指明分配 pagepool 内存的数量,也就是说,如果MaxFreeTcbs = 1000 , 则 pagepool 的内存数量为 500KB那么 MaxHashTableSize 应大于 500 才行。这个数量越大,则Hash table 的冗余度就越高,每次分配和查找 TCP  连接用时就越少。这个值必须是2的幂,且最大为65536.
 
IBM WebSphere Voice Server 在windows server 2003 下的典型配置
MaxUserPort = 65534 (Decimal)
MaxHashTableSize = 65536 (Decimal)
MaxFreeTcbs = 16000 (Decimal)
这里我们可以看到 MaxHashTableSize 被配置为比MaxFreeTcbs 大4倍,这样可以大大增加TCP建立的速度。

http://my.oschina.net/OutOfMemory/blog/95803

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326975201&siteId=291194637