473,756 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.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

Nov 15 '05 #1
3 10399
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)paldin o=at=exisconsul ting<dot>com

"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

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.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

Nov 15 '05 #3

"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


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
3893
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 support for sockets to the system. Thread needs to unblock when: - there is socket ready to be read, or
2
2601
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 readline() to get the response. But since these are blocking calls, I have to use a seperate thread for the input reading. But then I have another problem, my input thread running in the following loop while not quit: str = ins.readline() .......
1
6981
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 questions about the implementation of Python sockets. (both methods use blocking sockets) Method 1: Calls socket.sendall(data) for each device in sequence, all in a single thread.
3
12261
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 nonblocking mode in c++? I did something ugly like --- c/c++ mixture --- mkfifo( "testpipe", 777);
3
3709
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 tried this I don't see any diffents, I am sending 10Mb and the Send/BeginSend command doesn't wait till the data is on the remotepoint. Can somebody pls. explain this. Regards Robert.
7
17410
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 local or the remote endpoint from the client-side socket. The *really* strange thing is that Socket.LocalEndPoint is null. According to the doc, that's impossible: reading the LocalEndPoint
0
1748
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 IPEndPoint(IPAddress.Parse(ip),portNum); oSocket.Blocking=false; oSocket.Connect(endPoint); ..... while (CountinueThread) {
1
2829
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 server? I would rather not use threading or non-blocking sockets. Using VB.Net 2005.
1
20641
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 I don't see the error): "System.IO.IOException: Unable to read data from the transport connection:A blocking operation was interrupted by a call to WSACancelBlockingCall"
0
10034
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.