zoukankan      html  css  js  c++  java
  • winsock 的 SO_REUSEADDR 选项

    by Nikolai Fetissov

    First, make sure you set SO_REUSEADDR option on the server socket to be able to re-start listening.

    Then, I'm guessing, your problem is that accept() blocks and you cant stop it when you need to, so you are killing the thread. Bad solution. The right answer here is asynchronous I/O, i.e. select() or poll() or their Windows counterparts. Take a look at Advanced Winsock Samples.

    A quick-and-dirty solution in a multithreaded app is to check some is_it_time_to_stop_accepting_connections flag before each accept(), then when it's time to stop, flip the flag and connect to the listening port (yes, in the same program). That will unblock the accept() and allow you to do proper closesocket() or whatever.

    But seriously, read up on asynchronous I/O.

    https://stackoverflow.com/questions/6970394/how-can-i-stop-restart-listening-and-accepting-on-a-server-socket-in-winsock2-c

    by cnicutar

    https://stackoverflow.com/questions/6960219/why-not-using-so-reuseaddr-on-unix-tcp-ip-servers

    Well, UNP (Stevens 2004) says:

    SO_REUSEADDR allows a listening server to start and bind its well-known port, even if previously established connections exist that use this port as their local port.

    All TCP servers should specify this socket option to allow the server to be restarted

  • 相关阅读:
    NFS 服务器实验
    ssh服务实验
    dhcp服务实验
    邮件服务器
    搭建 DNS 服务器
    搭建 web 服务器
    Feign下的数据传递
    基于Spring Cloud Feign的Mock工具
    Git 使用注意事项
    基于redisson的延迟队列
  • 原文地址:https://www.cnblogs.com/liujx2019/p/13680513.html
Copyright © 2011-2022 走看看