views:

198

answers:

3

I want to close a socket so I can reopen one on the same port but I do not have a handle on that socket.

How can I get the socket that is listening on localhost:873 to close it?

+1  A: 

Without a kernel driver this is not possible. It is not legal in Windows to grab a socket handle in another process and close it proactively.

JaredPar
A: 

You could investigate SO_REUSEADDR.

Daniel Earwicker
This will allow multiple people to bind but you won't be able to close the other handle.
JaredPar
+1  A: 

Only that process that owns the socket can close it, so all you could try is ending the process that owns the socket.

From the command line you can find the Process ID of the process using a particular socket using the -o option to netstat. For example:

netstat -noa | findstr LISTENING

I don't know how you do this programmatically in .NET though.

Dave Webb
The application owned the socket, but it's went out of scope.
Malfist
@Malfist: Then the (only) solution is to keep track of it somehow.
Henk Holterman
Well, it's ordered to close, and then it goes out of scope but nestat still lists it as listening
Malfist