473,394 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 10363
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tero Saarni | last post by:
Hi, I have several threads communicating with each other using events stored in Queues. Threads block on Queue.get() until somebody publishes an event in thread's event queue. I need to add...
2
by: J. Xu | last post by:
I am a newbie to python. I am writing a client application, I need to read query results from the server. Mostly I'll have a file object for the connection to the server. So I use read() or...
1
by: Tim Black | last post by:
My application requires sending a large piece (~2MB) of data to several devices on a network via TCP sockets. I have experimented with different methods for doing this and this has raised some...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
3
by: Robert A. van Ginkel | last post by:
In news:OZ0W9RsdDHA.2432@TK2MSFTNGP10.phx.gbl... I ask the question how I can see if all the data is on the other side of the connection. I got as answer that I should use the blocking property. I...
7
by: Michi Henning | last post by:
Hi, I'm using a non-blocking connect to connect to a server. Works fine -- the server gets and accepts the connection. However, once the connection is established, I cannot retrieve either the...
0
by: user | last post by:
Hello I have socket: System.Net.Sockets.Socket oSocket = new Socket(IPAddress.Any.AddressFamily,SocketType.Stream,ProtocolType.Tcp); IPEndPoint endPoint = new...
1
by: opi | last post by:
My blocking TCP server hangs in the Accept method when the client software sometimes gets an error. Are there any ways to stop this blocking in the Accept method so I dont have to restart the...
1
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the error on server side(client side code is same, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.