Port reuse is a classic problem in network programming, and the knowledge points here are very cumbersome. This article briefly introduces SO_REUSEADDR through code, but does not involve SO_REUSEPORT. For a long time, we have all known that we cannot listen to the same port. For example, the following code. server1 .listen ( 8080 ) ; We will see the error Address already in use. But is it really impossible to bind to the same port? Not necessarily. #include < stdio .h > The above code starts two servers, both of which are bound to the same port. The compilation and execution can run normally because I specified different IPs. It can be seen that we usually think that multiple servers cannot listen to the same port at the same time because we only specify the port but not the IP. const net = require ( 'net' ) ; Execute the above code and you can see the bound address *:8080 through lsof -i:8080. In other words, if we do not specify an IP, the system will listen to all IPs by default. When listening to the same port for the second time, an error will be reported. Let's look at the second case. #include < stdio .h > The above code will report an error "Address already in use" when executed. Why does it not work when it is changed to INADDR_ANY? Because INADDR_ANY represents all IPs, so it cannot be bound to other IPs by default. Logically, when the operating system receives the data packet of 127.0.0.1:6666, it does not know who to give it to because both bound addresses are hit. But we can tell the operating system to whom to give this data packet. #include < stdio .h > The above code adds the logic of SO_REUSEADDR and compiles and executes successfully. It can be seen that SO_REUSEADDR tells the operating system who should handle a data packet when it hits multiple sockets. After the operating system has clarified this logic, it naturally allows the port to be listened in this way. |
<<: It took two years for 5G messaging to be officially commercialized. Is that it?
>>: Expectations for Network as a Service (NaaS) Technology
At present, the trend of digitalization is sweepi...
80VPS is a long-established Chinese hosting compa...
Yecaoyun has launched its 618 mid-year promotion,...
What is non-cellular 5G? I imagine most readers a...
Last week we shared CloudCone's VPS hosting B...
OneTechCloud was founded in 2019 and mainly provi...
As we stand on the cusp of a new year, the tech i...
HostYun has launched a new product, this time it ...
iOVZ Cloud has launched a promotion during the 20...
Everyone knows that when flying, you should turn ...
TLS v1.2 was released in August 2008. Ten years l...
The latest news is that BandwagonHost has opened ...
[[442879]] Hello everyone, I am Xuanyuan. That da...
Some people say that 5G network speed is 10-100 t...
"With 43 million analog mobile phone users, ...