Number of socket ports

Source: Internet
Author: User

Linux socket uses a 16-bit unsigned integer to indicate the port number. The maximum value is 65535. There is a classic misunderstanding about the port number. Because the port number is limited, a client can establish a maximum of 65536 socket connections, but this is not the case. The port can be reused.

 

A socket connection is a four-tuple consisting of [srcip, srcport, destip, destport]. If the Protocol (TCP, UDP, rawsocket, etc.) is a quintuple, In this four-tuple, as long as one element is different, two connections can be distinguished;Most of the problems we encounter with insufficient ports usually occur in the stress testing environment. When the number of connections exceeds a certain number, a client is used to establish a connection to a server, the system prompts cannot assign requested address when you connect.In such a scenario, destport and destport of the quad-tuples are determined, and srcip of a single Nic is also determined (regardless of the virtual Nic). Only srcport can be changed, the port value can only be 0-65535, and some reserved ports of the system are unavailable. Therefore, the number of connections between the client and the specific service is limited by the number of ports, when connecting to other servers, local ports can be reused. In fact, if no port is specified, connect to the server. The available port number is configured in/proc/sys/NET/IPv4/ip_local_port_range. The default value is 32768-61000.

 

In order to verify source port reuse, make the following small experiment. First, set the local port range to 60001 61000 to avoid Overconsuming OS resources, that is, only 1000 available port numbers are provided; write a small program that connects to the server and establish 900 connections to each of the two different services. The total number of connections exceeds 1800 of the available port 1000. After the Connection Program is executed, it is found through netstat that, all the 1800 connections are successfully established. The following output shows that the source port can be reused, and the number of port numbers does not exist.

  • TCP 0 0X. x.36.208: 60513X. x.36.204: 3200 established off (0.00/0/0)
  • TCP 0 0 X. x.36.208: 60513X. x.36.203: 3200 established off (0.00/0/0)

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.