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);
}
}
}
--
William Stacey, DNS MVP
"Logan McKinley" <lo***@globalweb.net> wrote in message
news:uK**************@TK2MSFTNGP12.phx.gbl...
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