469,609 Members | 1,687 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

Blocking with TCP Sockets

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

Nov 15 '05 #1
3 10062
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.

--
- Nicholas Paldino [.NET/C# MVP]
- nick(dot)paldino=at=exisconsulting<dot>com

"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

Nov 15 '05 #2
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

Nov 15 '05 #3

"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


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

David
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Tero Saarni | last post: by
2 posts views Thread by J. Xu | last post: by
3 posts views Thread by Mario | last post: by
3 posts views Thread by Robert A. van Ginkel | last post: by
7 posts views Thread by Michi Henning | last post: by
reply views Thread by user | last post: by
1 post views Thread by opi | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.