我知道那个SO_REUSEADDR是在线程关闭没释放的时候直接重新监听的但是看到了这个回答后感觉很困惑哪里理解不对请指出强调一下问题:
多个进程能※同时※监听同一端口吗?多个socket能※同时※监听同一端口吗?

解决方案 »

  1.   

    如果SO_REUSEPORT的描述无误的话,那么,可以,可以。
      

  2.   


           SO_REUSEPORT (since Linux 3.9)
                  Permits  multiple  AF_INET  or  AF_INET6  sockets to be bound to an identical socket address.  This
                  option must be set on each socket (including the first socket) prior  to  calling  bind(2)  on  the
                  socket.   To prevent port hijacking, all of the processes binding to the same address must have the
                  same effective UID.  This option can be employed with both TCP and UDP sockets.              For TCP sockets, this option allows accept(2) load distribution in a multi-threaded  server  to  be
                  improved  by using a distinct listener socket for each thread.  This provides improved load distri‐
                  bution as compared to traditional techniques such using a single accept(2)ing thread that  distrib‐
                  utes connections, or having multiple threads that compete to accept(2) from the same socket.              For  UDP  sockets,  the use of this option can provide better distribution of incoming datagrams to
                  multiple processes (or threads) as compared to the traditional technique of  having  multiple  pro‐
                  cesses compete to receive datagrams on the same socket.
      

  3.   

    SO_REUSEPORT (since Linux 3.9) 内核3.9
    Permits  multiple  AF_INET  or  AF_INET6  sockets to be bound to an identical socket address.
    允许多个IP或者IPv6的socket绑定到同一个socket地址。
    This option must be set on each socket (including the first socket) prior  to  calling  bind(2)  on  the socket.
    该选项必须在调用bind之前设置到每个socket上(包括第一个socket)。
    To prevent port hijacking, all of the processes binding to the same address must have the same effective UID.
    为了防止劫持,绑定到同一地址的所有进程要有相同的EUID。
    This option can be employed with both TCP and UDP sockets.
    TCP和UDP socket都可以使用该选项。For TCP sockets, this option allows accept(2) load distribution in a multi-threaded  server  to  be improved  by using a distinct listener socket for each thread.
    对TCP socket,该选项可以改善多线程服务器中的accept负载分发,通过给每一个线程一个不同的监听socket。
    This provides improved load distribution as compared to traditional techniques such using a single accept(2)ing thread that  distributes connections, or having multiple threads that compete to accept(2) from the same socket.
    比起传统的做法,比如用单一的线程来分发连接,或者在多线程中accept相同的socket来竞争,该选项可以改善负载分发。              For  UDP  sockets,  the use of this option can provide better distribution of incoming datagrams to
                  multiple processes (or threads) as compared to the traditional technique of  having  multiple  pro‐
                  cesses compete to receive datagrams on the same socket.
    对UDP socket,使用该选项可以提供更好的多进程/线程中的数据包分发;传统的做法是多进程在同一个socket上竞争接受数据包。
      

  4.   

    当TCP执行一个主动关闭,并发回最后一个ACK,该连接必须在 TIME_WAIT状态停留的时间为 2倍的MSL。这样可让 TCP再次发送最后的ACK以防这个ACK丢失(另一端超时并重发最后的FIN)。这个期间socket不能被使用,但是在服务需要处理大量请求时,为提高服务器的吞吐率,可以设置SO_REUSEADDR重用TCP状态为TIME_WAIT的socket