handling sockets when application crashes RRS feed

  • Question

  • I have written a Windows Service in c# that uses a socket to communicate with a desktop application (for diagnostics).

    The problem I am having is that if the service crashes and you try to restart it, you get errors and a failed start as the socket are already bound to another applications and can't be rebound <don't have it exact, the message got deleted>.  Its in the windows application event viewer.   Both apps and processes can be killed off and you still can't start the service again.  How can you have the windows service you create take ownership of the sockets ?  pointers to the relevant webpage would help.  Thanks.  Google searches weren't so helpful as I couldn't come up with the correct search terms to turn up a relevant nugget. 

    Thursday, September 19, 2019 4:46 PM

All replies

  • Hello,

    Have you tried a graceful recovery in OnShutDown event and setting the ExitCode? Also have you tried subscribing to AppDomain.CurrentDomain.UnhandledException to handles the used sockets? 

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Thursday, September 19, 2019 5:04 PM
  • Which APIs are you using to create the socket?  What you need to do is set the SO_REUSEPORT flag on the socket.  In the C world, you'd use the setsockopt API to set that flag.  I'd need to know how you are creating the socket to advise you on the equivalent.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Thursday, September 19, 2019 5:39 PM
  • I'm using TcpListener serverSocket = new TcpListener(localAddr,port)

    The socket listener is on its on thread.

    I do subscribe to the AppDomain.CurrentDomain.UnhandledException and in my handler do some error logging, but how do I "handle" the used sockets ?

    Also, any pointers on graceful handling in the OnShutdown event would much be appreciated.  

    Sorry for the newbie stuff but was thrown in the deepend on this.

    thanks a bunch.

    Thursday, September 19, 2019 7:33 PM
  • Hi Ksukat63,

    First, I want to know if you set try catch for the socket program.

    Second, I also find the following link may help you.

    C# TCP Listener crashing

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Friday, September 20, 2019 5:56 AM
  • I do this:

                    Int32 port;
                    port = Convert.ToInt32(socket);
                    IPAddress localAddr = IPAddress.Parse(serverip);
                    TcpListener serverSocket = new TcpListener(localAddr, port);


    on the inner try catch I write messages to the windows app log on the outer try catch I do the same thing.  What should I do with the socket to "clean up" on the catch ?

    sorry for slow response.

    Friday, September 20, 2019 7:51 PM
  • It's not clear from your posts, so for clarification, could you tell us if communication is going from server to client, from client to server or in both directions?

    ~~Bonnie DeWitt [C# MVP]

    Sunday, September 22, 2019 12:02 AM
  • In C# it would be equivalent to something like this:

    TcpClient client = new TcpClient();
    client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
    client.Connect(new IPEndPoint(IPAddress.Parse(""), 80));

    • Proposed as answer by Tim Roberts Tuesday, September 24, 2019 4:33 AM
    Monday, September 23, 2019 1:34 AM