Blocking with TCP Sockets

  • Thread starter Thread starter Logan McKinley
  • Start date Start date
L

Logan McKinley

I have a C# program that uses blocking sockets and want to allow the user to
stop the server. The problem I am having is the socket blocks on
--------------------------------------------------------------
listener = new System.Net.Sockets.TcpListener(6254);
listener.Start();
//skt is a socket
skt =listener.AcceptSocket(); <--- this line blocks
--------------------------------------------------------------
I attempt to stop the thread as follows
--------------------------------------------------------------
if (thListener.ThreadState != System.Threading.ThreadState.Stopped)
//thListener is the offending thread
{
try{listener.Stop();} //this always works
catch{System.Diagnostics.Trace.WriteLine("issue");}
thListener.Abort();
thListener.Join(2000);
}
--------------------------------------------------------------
I get the following error
--------------------------------------------------------------
System.Net.Sockets.SocketExchange: A blocking operation was interrupted by a
call to WSACancelBlockingCall
at System.Net.Sockets.Socker.Accept()
at System.Net.Sockets.TcpListener.AcceptSocket()
--------------------------------------------------------------
this error is in an OK-only popup box with no title and appears to be coming
out of the "listener" thread

I have not been able to find any information onWSACancelBlockingCall and was
hoping someone else has an idea how to prevent that popup.
Thanks in advance,
~Logan
 
Logan,

You actually get a messagebox popping up from the other thread that is
not your own? Can you condense this into a sample project? It sounds like
a definite bug in the framework, and if that is the case, someone from MS
will pick it up here, or we (the MVPs) can escalate it for you.
 
This works like normal for me. I have Framework 1.1 on 2003 server. I did
have to call Thread.Sleep(0) to allow the listener thread to startup and
listen before I tried to close it. Also, I catch the abort Exception inside
the thread (i.e. Start()) hth.
....
private void button11_Click(object sender, System.EventArgs e)
{
ServerClass serverClass = new ServerClass();
TcpListener listener = serverClass.listener;
Socket skt = serverClass.skt;
Thread thread = new Thread(new ThreadStart(serverClass.Start));
thread.Start();
while( !(thread.ThreadState == ThreadState.Running) );
Console.WriteLine("Thread State:"+thread.ThreadState.ToString());
Thread.Sleep(0); //let it startup and listen.
listener = serverClass.listener;

if (thread.ThreadState != ThreadState.Stopped)
//thListener is the offending thread
{
try
{
listener.Stop();
Console.WriteLine("Listener stopped.");
} //this always works
catch
{
Console.WriteLine("Could not stop listener.");
}
Console.WriteLine("About to Abort the serverClass thread.");
thread.Abort();
Console.WriteLine("ServerClass thread sent abort signal.");
Console.WriteLine("Thread State:"+thread.ThreadState.ToString());
thread.Join();
Console.WriteLine("Threads Joined!");
}

}

public class ServerClass
{
public TcpListener listener;
public Socket skt;

public void Start()
{
try
{
IPAddress localip = IPAddress.Any;
listener = new System.Net.Sockets.TcpListener(localip, 6256);
listener.Start();
Console.WriteLine("listener Started, blocking on acceptsocket.");
skt = listener.AcceptSocket();
Console.WriteLine("After AcceptSocket().");
}
catch (Exception e)
{
Console.WriteLine("Exception in ServerClass.Start(): "+e.Message);
}
}
}
 
Logan McKinley said:
I have a C# program that uses blocking sockets and want to allow the user to
stop the server. The problem I am having is the socket blocks on
--------------------------------------------------------------
listener = new System.Net.Sockets.TcpListener(6254);
listener.Start();
//skt is a socket
skt =listener.AcceptSocket(); <--- this line blocks
--------------------------------------------------------------
I attempt to stop the thread as follows
--------------------------------------------------------------
if (thListener.ThreadState != System.Threading.ThreadState.Stopped)
//thListener is the offending thread
{
try{listener.Stop();} //this always works
catch{System.Diagnostics.Trace.WriteLine("issue");}
thListener.Abort();
thListener.Join(2000);
}
--------------------------------------------------------------
I get the following error
--------------------------------------------------------------
System.Net.Sockets.SocketExchange: A blocking operation was interrupted by a
call to WSACancelBlockingCall
at System.Net.Sockets.Socker.Accept()
at System.Net.Sockets.TcpListener.AcceptSocket()

This is correct. Something has to unblock your listener thread. And the
exception does that. Just catch it in listener, and end gracefully.

David
 
Back
Top