blogdaren 2019-04-19 抢沙发 106人次


    A:通过 fork 子进程继承父进程监听句柄;
    B:通过 socket的 SO_REUSEADDR 属性实现端口复用;

2、对于windows,使用的也是 socket的 SO_REUSEADDR【注意windows这个选项默认是激活的】。

Windows only knows the SO_REUSEADDR option, there is no SO_REUSEPORT. Setting SO_REUSEADDR on a socket 
in Windows behaves like setting SO_REUSEPORT and SO_REUSEADDR on a socket in BSD, with one exception: 
A socket with SO_REUSEADDR can always  bind to exactly the same source address and port as an already 
bound socket,  even if the other socket did not have this option set when it was bound. 
This behavior is somewhat dangerous because it allows an application "to steal" the connected port 
of another application. Needless to say, this can have major security implications. 
Microsoft realized that this might be a problem and thus added another socket option SO_EXCLUSIVEADDRUSE. 
Setting SO_EXCLUSIVEADDRUSE on a socket makes sure that if the binding succeeds, 
the combination of source address and port is owned exclusively by this socket and no other socket 
can bind to them, not even if it has SO_REUSEADDR set.

版权声明:除非注明,本文由( blogdaren )原创,转载请保留文章出处。