By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,497 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,497 IT Pros & Developers. It's quick & easy.

Aborting Socket BeginAccept Async Call

P: n/a


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(localEP)
s.Listen(10)

The second line should be "listener.Listen(10)"..

Anyhow, their "server" loop looks like this:

While True
allDone.Reset()

Console.WriteLine("Waiting for a connection...")
listener.BeginAccept(New _
AsyncCallback(SocketListener.acceptCallback), _
listener)

allDone.WaitOne()
End While
Notice the "BeginAccept" 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(AddressOf OnServerAccept)
y = m_ServerSocket.BeginAccept(x, Nothing)

'Wait For Event
m_ServerThreadEvent.WaitOne()

'OK, Reset Event
m_ServerThreadEvent.Reset()

Loop

'NEED TO ABORT THE "BeginAccept" 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 "BeginAccept". 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.BeginAccept()"
call that I want to abort because I am closing the socket down?

Thanks for any help!
// CHRIS

Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.BeginAccept()"
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

Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.