How to close a socket left open by a killed program?
Solution 1
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))
Solution 2
You might want to try using Twisted for your networking. Mike gave the correct low-level answer, SO_REUSEADDR
, but he didn't mention that this isn't a very good option to set on Windows. This is the sort of thing that Twisted takes care of for you automatically. There are many, many other examples of this kind of boring low-level detail that you have to pay attention to when using the socket module directly but which you can forget about if you use a higher level library like Twisted.
Solution 3
You are confusing sockets, connections, and ports. Sockets are endpoints of connections, which in turn are 5-tuples {protocol, local-ip, local-port, remote-ip, remote-port}. The killed program's socket has been closed by the OS, and ditto the connection. The only relic of the connection is the peer's socket and the corresponding port at the peer host. So what you should really be asking about is how to reuse the local port. To which the answer is SO_REUSEADDR as per the other answers.
Related videos on Youtube
Comments
-
Mr. Shickadance about 4 years
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 will directly kill it, and in either case the socket may be left open for some unknown time.
The next time I go to run the program I get this error:
socket.error: [Errno 98] Address already in use
Now the program always tries to use the same port, so it appears as though it is still open. I checked and am quite sure the program isn't running in the background and yet my address is still in use.
SO, how can I manually (or otherwise) close a socket/address so that my program can immediately re-use it?
Update
Based on Mike's answer I checked out the
socket(7)
page and looked at SO_REUSEADDR:SO_REUSEADDR Indicates that the rules used in validating addresses supplied in a bind(2) call should allow reuse of local addresses. For AF_INET sockets this means that a socket may bind, except when there is an active listening socket bound to the address. When the listen‐ ing socket is bound to INADDR_ANY with a specific port then it is not possible to bind to this port for any local address. Argument is an integer boolean flag.
-
Michael Mrozek about 13 yearsAny opposition to moving this to SO? It ended up being an entirely programming problem, so it probably makes more sense there
-
Mr. Shickadance about 13 yearsNot at all, I only posted this here because I was thinking of a Linux command to close the socket, I agree its programming material. Perhaps the title could be changed to reflect that it was a Python program.
-
xenoterracide about 13 years@Mr. Shickadance migrated, you'll have to reword the title if you think it's necessary.
-
winbina about 13 years
-
-
Mr. Shickadance about 13 yearsI believe you mean
setsockopt
, and I tried it but still no luck. I'll report back after more testing. -
Mike Pennington about 13 years@Mr. Shickadance, yes it is
setsockopt()
... I mistyped -
Mr. Shickadance about 13 yearsI was wrong, this indeed solved the problem...and that's why I mentioned more testing! I wasn't pushing the new, modified code before I ran the test (not sure how I forgot). Thanks!
-
Mike Pennington about 13 years@Mr. Shickadance, you are most welcome... good luck with your project
-
Jean-Paul Calderone about 13 yearsOn Windows, REUSEADDR lets a process take over a listening port from another still running process.
-
Sudo Bash about 11 yearsWell this was migrated from unix.stackexchange.com, so windows is probably not a large concern. Good point anyway.+1
-
Goncalo almost 10 yearsIn fairness, your answer should have been a comment to Mike's answer. Thanks for the tidbit.
-
Ginger about 3 yearsI wish I could upvote an answer more than once! This is a lifesaver!