Socket listen doesn't unbind in C++ under linux

后端 未结 3 806
自闭症患者
自闭症患者 2021-01-11 17:12

I have a socket that listens on some port. I send the SIGSTOP signal to the thread that waits on the port (using accept) and terminate it. then I close the fd of the socket

相关标签:
3条回答
  • 2021-01-11 17:26

    I think the problem is that you have not properly closed the socket and/or your program.The socket probably still exists in the OS. check it with something like nestat -an. You should also check if your process has exited. If it has correctly ended, it should have closed your socket.

    What you should do is :

    • interrupt your thread with a signal.
    • when interrupted your thread should cleanly close the socket before the end.
    • then you can cleanly exit from your program.

    my2cents,

    0 讨论(0)
  • 2021-01-11 17:47

    Did you know that sockets are typically kept in a kind of limbo for a minute or two after you've finished listening on them to prevent communications intended for the previous process coming to yours? It's called the 'TIME_WAIT' state.

    If you want to override that behaviour use setsockopt to set the SO_REUSEADDR flag against the socket before listening on it.

    0 讨论(0)
  • 2021-01-11 17:47

    See: Using SO_REUSEADDR - What happens to previously open socket?

    0 讨论(0)
提交回复
热议问题