0

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?
4

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.

http://www.beej.us/guide/bgnet/output/html/multipage/setsockoptman.html

https://stackoverflow.com/questions/9207781/error-in-binding-port-address-already-in-use-tcp-socket-programming-in-unix

https://stackoverflow.com/questions/10071107/rebinding-a-port-to-datagram-socket-on-a-difftent-ip

| 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.