How to close a socket left open by a killed program?

后端 未结 3 1836
执念已碎
执念已碎 2020-12-01 03:33

I have a Python application which opens a simple TCP socket to communicate with another Python application on a separate host. Sometimes the program will either error or I w

3条回答
  •  [愿得一人]
    2020-12-01 04:04

    Assume your socket is named s... you need to set socket.SO_REUSEADDR on the server's socket before binding to an interface... this will allow you to immediately restart a TCP server...

    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind((ADDR, PORT))
    

提交回复
热议问题