When running a code that is supposed to read from a TCP/IP port I often get the following message (depending exactly what language have been used):

socket_bind(): unable to bind address

I know that this happens when some other process listens to this port / is binded to this port (I do not know the exact phrase to describe), and therefore my code cannot listen to that port. However, it is my code which I just stopped (by CTRL-C) that is blocking this port! There seem to be some timeout involved, and after waiting some time (about 30 seconds) I can start my code to listen on this specific port without problems.

I would like to know:

  • Why is the port still blocked although I have stopped the code?
  • How can I force the system to release this port so I can restart my code and start listening to this port?

Assuming you're talking C/C++, use setsockopt() and SO_REUSEADDR. This allows reuse as long as there is no active process listening to that port.

Edit: The reason it is still in use is you didn't close the socket down appropriately. You Control-C killed it. You can use netstat to see the ports that are open or not quite closed yet.




| improve this answer | |
  • Thank you very much. Although it is php and/or python, I can look for the corresponding option now. – Alex Apr 12 '13 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.