Port reuse in Linux solves the problem

Source: Internet
Author: User
In Linux, port multiplexing solves the problem of Linux Enterprise Application-Linux server application information. The following is a detailed description. Author: lost in the past summer -- revealing and enjoying the cloud

Solution reference: Currently, Linux does not allow reuse of same local port for both TCP server and TCP client. in addition, the small test program can be successful, and the difference between the test program and my library is that, So accept the connection and the port that initiates the connection can be separated.

Other materials:

This is the biggest mess: http://www.ajaxstu.com/Linuxbiancheng/294968.html Excuse me: linux does not have port reuse? I cannot find it using SO_REUSEPORT

You have done this. You need To modify/usr/include/asm/socket. h:/* To add: # define SO_REUSEPORT 15 */

Http://hi.baidu.com/qiaoyong/blo... b1f4108ff47707.html

Call Summary of freebsd and bind systems in linux

Only AF_INET is considered (the same port indicates that both the IP address and the port number are the same)
1. freebsd supports SO_REUSEPORT and SO_REUSEADDR options, while linux only supports SO_REUSEADDR options.
2. in freebsd, The SO_REUSEPORT option can be used to bind two tcp sockets to the same port. Similarly, the SO_REUSEPORT option can be used to bind two udp sockets to the same port.
3. in linux, the two tcp sockets cannot be bound to the same port. If the SO_REUSEADDR option is used, the two udp sockets can be bound to the same port.
4. in freebsd, two tcp sockets are bound to the same port, and only the first socket obtains data.
5. in freebsd, two udp sockets are bound to the same port. If the destination address of the data packet is a unicast address, only the first socket obtains data. If the destination address of the data packet is a multicast address, the two sockets obtain the same data at the same time.
6. in linux, two udp sockets are bound to the same port. If the destination address of the data packet is a unicast address, only the last socket obtains data. If the destination address of the data packet is a multicast address, the two sockets obtain the same data at the same time.

Http://cache.baidu.com/c? M = 9d78d... 7 & user = baidu & fast = y
Unix Network API

SO_REUSEADDR and SO_REUSEPORT
SO_REUSEADDR provides the following functions:
SO_REUSEADDR allows you to start a listener server and bind its well-known port, even if the previously established connection to use this port as their local port still exists. This usually occurs when the listening server is restarted. If this option is not set, an error occurs during bind.
SO_REUSEADDR allows multiple instances of the same server to be started on the same port, as long as each instance is bound with a different local IP address. For TCP, it is impossible to start multiple servers that bind the same IP address and the same port number.
SO_REUSEADDR allows a single process to bind the same port to multiple sets of interfaces, as long as each bundle specifies a different local IP address. This is generally not used for TCP servers.
SO_REUSEADDR allows completely repeated binding: When an IP address and port are bound to a set of interfaces, the IP address and port are also allowed to be bound to another set of interfaces. Generally, this feature is available only on systems that support multicast, and only for UDP interfaces (TCP does not support multicast ).
The SO_REUSEPORT option has the following semantics:
This option allows completely repeated binding, but this option is only available when you specify this option for all the interfaces that want to bind the same IP address and port.
If the bound IP address is a multicast address, SO_REUSEADDR is equivalent to SO_REUSEPORT.
Suggestions for using these two sets of Interface Options:
In all TCP servers, set the SO_REUSEADDR interface option before calling bind;
When writing a multicast application that can run multiple times on the same host at the same time, set the SO_REUSEADDR option and bind the multicast address of this group as the local IP address.

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.