WARNING: Verbosity: skip to the very bottom paragraph for succinct
version of my question.)
Hi-
I can't seem to find an answer to this. I am playing around with a
variation of the ".NET Framework Developer's Guide" (in the MSDN docs)
"Using an Asynchronous Server Socket" sample.
As usual with docs like this, the example isn't very good. I had to
fix some typo's in the code ..
Like:
listener.Bind(l ocalEP)
s.Listen(10)
The second line should be "listener.Liste n(10)"..
Anyhow, their "server" loop looks like this:
While True
allDone.Reset()
Console.WriteLi ne("Waiting for a connection...")
listener.BeginA ccept(New _
AsyncCallback(S ocketListener.a cceptCallback), _
listener)
allDone.WaitOne ()
End While
Notice the "BeginAccep t" call....
And notice that it's an INFINITE LOOP! Nice style..
Okay, Here's my version at the moment:
Dim x As AsyncCallback
Dim y As IAsyncResult
'Wait For Client Connections or Server Stop
Do While m_bStopping = False
'Asynchronous Accept Connection
x = New AsyncCallback(A ddressOf OnServerAccept)
y = m_ServerSocket. BeginAccept(x, Nothing)
'Wait For Event
m_ServerThreadE vent.WaitOne()
'OK, Reset Event
m_ServerThreadE vent.Reset()
Loop
'NEED TO ABORT THE "BeginAccep t" CALL
'At this point, after the loop, we are stopping the server
Debug.WriteLine ("Closing Server Socket")
m_ServerSocket. Close()
m_bRunning = False
m_bStopping = False
After the loop and "m_ServerSocket .Close()" is called, my
"OnServerAccept " method gets invoked! NOT because anyone has
connected to the server port, it's apparently what happens when you
close the socket that has a pending "BeginAccep t". The server socket
is CLOSED (and disposed) by the time "OnServerAccept " is invoked, so
the method can't call "m_ServerSocket .EndAccept(ar)" to finish the
BeginAccept/EndAccept pair. [it just takes and catches an exception
"Object Disposed"]
When I run the program, start the server, then stop the server, the
"OnServerAccept " gets invoked ONCE. When I start the server again,
then stop it, "OnServerAccept " gets invoked TWICE.. and if I repeat
the Start Server / Stop Server again (any number of times more), it
always calls the "OnServerAccept " TWICE.
Here's my debug output:
<STARTING SERVER>
<STOPPING SERVER>
Closing Server Socket
The thread 'Server Thread' (0x7b8) has exited with code 0 (0x0).
OnServerAccept
<STARTING SERVER>
<STOPPING SERVER>
Closing Server Socket
The thread 'Server Thread' (0xa58) has exited with code 0 (0x0).
OnServerAccept
OnServerAccept
<STARTING SERVER>
<STOPPING SERVER>
Closing Server Socket
The thread 'Server Thread' (0xa38) has exited with code 0 (0x0).
OnServerAccept
OnServerAccept
<STARTING SERVER>
<STOPPING SERVER>
Closing Server Socket
The thread 'Server Thread' (0x618) has exited with code 0 (0x0).
OnServerAccept
OnServerAccept
<STARTING SERVER>
<STOPPING SERVER>
Closing Server Socket
The thread 'Server Thread' (0x9f8) has exited with code 0 (0x0).
OnServerAccept
OnServerAccept
In my code above, I use the variables x and y. After execution drops
out of the Do/While loop, I have that:
y.IsCompleted() = False
... which usually would be the case, unless the server was stopped JUST
after a new connection was established, before the do-while loop could
loop around.
So I guess my question is "How can I cancel a "socket.BeginAc cept()"
call that I want to abort because I am closing the socket down?
Thanks for any help!
// CHRIS 1 6277
On Wed, 31 Dec 2003 22:35:46 -0500, Chris Morse
<ch***@sorry.no .spam.com> wrote: So I guess my question is "How can I cancel a "socket.BeginAc cept()" call that I want to abort because I am closing the socket down?
Thanks for any help! // CHRIS
Searching in the .csharp newsgroup, it seems that it can't be done.
Someone else asked the same question, and a C# MVP responded that the
..NET Framework does not support cancelling async callbacks. If you
want the option to abort, you have to write something yourself.
// CHRIS This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Rob |
last post by:
Hello,
I've got a huge problem with async sockets.
I've created a networked virtual environment, using async socket programming.
The problem I face occurs when I close the listener socket. It listens using socket.BeginAccept(...).
When a client disconnects, the socket is disconnected like this:
try
{
socket.Shutdown(SocketShutDown.Receive);
|
by: User |
last post by:
Hi,
How do I pool my socket to see if there is any message pending?
I don't want to use timers because they are multithreaded and it break
the linearity of my messages. (incoming message are numbered and it is
important to keep the order)
Can I add a handler to the socket? So it could react by itself when
there is pending data? Something like that the winsock (in VB6) was doing.
|
by: John Sheppard |
last post by:
Hi all,
I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose but have been unsuccessful in finding the answers so far. Either because my understanding of sockets isn't where it needs to be or my questions are too basic.
My programming environment is Windows XP, Visual Studio .NET 2003 and C#.
So here it...
|
by: Droopy Toon |
last post by:
Hi,
I am using asynchronous socket (BeginAccept for example).
I tried to name each thread I am using but threads created by
asynchronous Socket functions (like BeginAccept) creates "anonymous"
threads.
I named the thread (see sample code below) in Accept callback started by
BeginAccept but all connections accepted run in a thread that has the
same name !
So, even a new thread is not started by BeginAccept, even my naming is
|
by: Engineerik |
last post by:
I am trying to create a socket server which will listen for connections from
multiple clients and call subroutines in a Fortran DLL and pass the results
back to the client. The asynchronous socket client and asynchronous socket
server example code provided in the .NET framework developers guide is a
great start but I have not dealt with sockets before and I am struggling with
something.
From what I can tell the sample server code ...
| |
by: darthghandi |
last post by:
I've created a class to listen to all interfaces and do a
BeginAccept(). Once it gets a connection, it passes the connected
socket off and stores it in a List. Next, it continues to listen for
more incoming connections and does the BeginAccpet() again. It does
an infinite loop this way. My question is: What is the best way to
stop this? I thought about putting a boolean in there, but then what
if it's still waiting for an incoming...
|
by: =?Utf-8?B?TWlrZVo=?= |
last post by:
I have a sync socket application. The client is blocked with
Socket.Receive(...) in a thread, another thread calls Socket.Close(). This
unblock the blocked thread. But the socket server is still connected. Any
idea?
Thanks.
|
by: ThunderMusic |
last post by:
Hi,
I'm currently working with sockets. I accept connections using
m_mySocket.Listen(BackLogCount); But when I want to stop listening, I
shutdown all my clients and call m_mySocket.Close(), but it always raise a
OnConnect event (actually, it calls the callback function as if there was a
new connection attempt) and I receive a ObjectDisposedException as soon as I
do m_mySocket.EndAccept.
Does anyone have any idea of what I could do about...
|
by: O.B. |
last post by:
I have a socket configured as TCP and running as a listener. When I
close socket, it doesn't always free up the port immediately. Even
when no connections have been made to it. So when I open the socket
again, the bind fails because the port is still in use. When I
execute the code in "debug" mode, the problem never occurs. When I
execute the same code in release mode, the problem appears about 20%
of the time.
Here's the code:
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
| |
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |