4

My server app runs perfectly fine until I try to close it. At that point i'm getting two exceptions. I put in a try-catch to try and debug them, and go the following:

The first exception is: A blocking operation was interrupted by a call to WSACancelBlockingCall and the 2nd one is Not listening. You must call the Start() method before calling this method.

Here's a snippet from where the exception occurs:

    private void ListenForClients()
    {
        this.tcpListener.Start();            

        while (true)
        {
            try
            {
                TcpClient client = this.tcpListener.AcceptTcpClient(); // EXCEPTION OCCURS HERE
                string clientIPAddress = "" + IPAddress.Parse(((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString());
                ClientIPLabel.Text = clientIPAddress;
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.Start(client);
            }
            catch (Exception e)

            {
                MessageBox.Show(e.Message.ToString());
            }                              
        }
    }

I'm not sure if it might be because I'm missing something here or not, but here's my code for closing the server app:

    private void ShutdownServer_Click(object sender, EventArgs e)
    {
        this.tcpListener.Stop();
            Application.Exit();
    }

I'm also unable to run this app from a release build; it just freezes as soon as I try to run it, and I need to restart my machine in order to close it (I can close the form, but the process stays running in task manager and "end task" doesn't close it). I'm assuming these exceptions are likely what's causing that issue, since I can run the debug build just fine.

Patrick
  • 430
  • 6
  • 21
  • The issue seems to be in the tcplistener, because if I comment out `this.tcpListener.Stop();` in my shutdown method, I don't get any exceptions, but even though my GUI closes, the process is still running in the background until I close it with the task manager – Patrick Dec 28 '15 at 22:57

1 Answers1

4

The exception is normal. It happens because you are concurrently closing a listener that is accepting at that time.

Catching that exception and throwing it away is the right choice. I would do it like this to make the catch safer:

//Stop:
isStopping = true;
this.tcpListener.Stop();

//...

private void ListenForClients()
{
    this.tcpListener.Start();            

    while (true)
    {
        try
        {
            TcpClient client = this.tcpListener.AcceptTcpClient();
            //...
        }
        catch (Exception e)
        {
            if (!isStopping) throw; //don't swallow too much!
        }                              
    }
}

There is no cleaner (exception free) way to interrupt socket operations. (Yes, this is a bad situation.)

Regarding the freezing issue, this is not normal and not a .NET issue (.NET can't prevent a process from being killed, there is no such Windows API). Windows bug, OS corruption, Anti Virus, Firewall, ...

usr
  • 168,620
  • 35
  • 240
  • 369
  • Hmm... that's strange about the freezing then. I keep my system very clean, and I've tried turning off AV and letting my program through the firewall. I get the same problem on release builds of both my client and server app, it's very disappointing to say the least, considering they run fine from the debug build. I'd rather not have to use that as a release. – Patrick Dec 28 '15 at 23:19
  • Maybe the freeze is simply a bug in your app? Pause the debugger during the freeze to see what's on the stack(s). – usr Dec 29 '15 at 13:14
  • Unfortunately not the case... I've started a new question for that issue [HERE](http://stackoverflow.com/questions/34502750/release-builds-hang-crash-regardless-of-code) – Patrick Dec 29 '15 at 16:26