views:

54

answers:

1

I have a service which communicates through tcpListener. Problem is when the user restarts the service - an "Address already in use" exception is thrown, and the service cannot be started for a couple of minutes or so.

Is there's any way of telling the system to terminate the old connection so I can open a new one? (I can't just use random ports because there is no way for the service to notify the clients what is the port, so we must depend on a predefined port)

+2  A: 

Set the SO_REUSEADDR socket option before binding to the listening port. It looks like the corresponding .NET code is something like:

SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
Greg Hewgill
Thanks. works great
Nissim