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.EventArg s e)
{
ServerClass serverClass = new ServerClass();
TcpListener listener = serverClass.lis tener;
Socket skt = serverClass.skt ;
Thread thread = new Thread(new ThreadStart(ser verClass.Start) );
thread.Start();
while( !(thread.Thread State == ThreadState.Run ning) );
Console.WriteLi ne("Thread State:"+thread. ThreadState.ToS tring());
Thread.Sleep(0) ; //let it startup and listen.
listener = serverClass.lis tener;
if (thread.ThreadS tate != ThreadState.Sto pped)
//thListener is the offending thread
{
try
{
listener.Stop() ;
Console.WriteLi ne("Listener stopped.");
} //this always works
catch
{
Console.WriteLi ne("Could not stop listener.");
}
Console.WriteLi ne("About to Abort the serverClass thread.");
thread.Abort();
Console.WriteLi ne("ServerClas s thread sent abort signal.");
Console.WriteLi ne("Thread State:"+thread. ThreadState.ToS tring());
thread.Join();
Console.WriteLi ne("Threads Joined!");
}
}
public class ServerClass
{
public TcpListener listener;
public Socket skt;
public void Start()
{
try
{
IPAddress localip = IPAddress.Any;
listener = new System.Net.Sock ets.TcpListener (localip, 6256);
listener.Start( );
Console.WriteLi ne("listener Started, blocking on acceptsocket.") ;
skt = listener.Accept Socket();
Console.WriteLi ne("After AcceptSocket(). ");
}
catch (Exception e)
{
Console.WriteLi ne("Exception in ServerClass.Sta rt(): "+e.Message );
}
}
}
--
William Stacey, DNS MVP
"Logan McKinley" <lo***@globalwe b.net> wrote in message
news:uK******** ******@TK2MSFTN GP12.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.Sock ets.TcpListener (6254);
listener.Start( );
//skt is a socket
skt =listener.Accep tSocket(); <--- this line blocks
--------------------------------------------------------------
I attempt to stop the thread as follows
--------------------------------------------------------------
if (thListener.Thr eadState != System.Threadin g.ThreadState.S topped)
//thListener is the offending thread
{
try{listener.St op();} //this always works
catch{System.Di agnostics.Trace .WriteLine("iss ue");}
thListener.Abor t();
thListener.Join (2000);
}
--------------------------------------------------------------
I get the following error
--------------------------------------------------------------
System.Net.Sock ets.SocketExcha nge: A blocking operation was interrupted by
a call to WSACancelBlocki ngCall
at System.Net.Sock ets.Socker.Acce pt()
at System.Net.Sock ets.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 onWSACancelBloc kingCall and
was hoping someone else has an idea how to prevent that popup.
Thanks in advance,
~Logan