Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a program that sometime misses to close a tcp connection.

After the program has ended I can see that the port is taken. TCPViewer shows "non-existent" in the "Process" column.

If i try to "End process" or "Close connection" nothing happens.

If i reboot the server the port is released. But how can I tell Windows to release this port without a reboot?

SOLUTION: It was dw20.exe (Microsoft Error Reporting) that was holding it back.

share|improve this question
    
What exactly do you mean by "release the port"? It doesn't make sense with connections. –  grawity Mar 16 '11 at 11:54
    
Which version of windows are you using? Have you tried starting tcpviewer as administrator? –  socken23 Mar 16 '11 at 11:56
    
I'm running windows 2008 server as admin –  Stig Mar 16 '11 at 14:14
add comment

1 Answer

The operating system will automatically close all TCP connections when a process exits1.

What you are seeing is probably a connection in the TIME-WAIT state ("...represents waiting for enough time to pass to be sure the remote peer received the acknowledgment of its connection termination request").

This should not be a problem for clients, as a host can establish multiple TCP connections to the same remote host:port.

For a server, SO_REUSEADDR can be used.

What exactly does SO_REUSEADDR do?

This socket option tells the kernel that even if this port is busy (in the TIME_WAIT state), go ahead and reuse it anyway. If it is busy, but with another state, you will still get an address already in use error. It is useful if your server has been shut down, and then restarted right away while sockets are still active on its port. You should be aware that if any unexpected data comes in, it may confuse your server, but while this is possible, it is not likely.

It has been pointed out that "A socket is a 5 tuple (proto, local addr, local port, remote addr, remote port). SO_REUSEADDR just says that you can reuse local addresses. The 5 tuple still must be unique!" by Michael Hunter (mphunter@qnx.com). This is true, and this is why it is very unlikely that unexpected data will ever be seen by your server. The danger is that such a 5 tuple is still floating around on the net, and while it is bouncing around, a new connection from the same client, on the same system, happens to get the same remote port. This is explained by Richard Stevens in ``2.7 Please explain the TIME_WAIT state.''.

See also this StackOverflow answer: Using SO_REUSEADDR - What happens to previously open socket?


1 Technically, when all handles to that connection are destroyed. Multiple processes may own a file handle.

share|improve this answer
    
i forgot to say that it is a host. Does that change anything? –  Stig Mar 16 '11 at 12:54
    
i get a AddressAlreadyInUseException when starting the program again –  Stig Mar 16 '11 at 13:07
    
@Stig: What do you mean by "host"? A program that listens for connections? –  grawity Mar 16 '11 at 14:21
    
@Stig: Answer updated to explain server sockets. –  grawity Mar 16 '11 at 14:30
    
Yes it is a program that listens for connections! A service waiting for clients to retrieve data. –  Stig Mar 16 '11 at 15:02
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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