Groups | Blog | Home
all groups > dotnet framework > june 2006 >

dotnet framework : How to Stop a Listening socket?



rossu
6/29/2006 1:52:01 PM
I'm trying to make a simple little asynchronous socket server. It accepts
connections, reads and writes, but I can't make it gracefully stop listening.

listener.listen(1)
listener.beginaccept(...)

If before anyone has attempted to connect, I try to stop the listener
socket, I get nasty errors, which I can trap, but it is not obvious that it
has actually stopped or that the asynchronous callback system has been
Jared Parsons [MSFT]
6/29/2006 6:41:56 PM
Hello rossu,

[quoted text, click to view]

How are you stopping the Socket?

--
Jared Parsons [MSFT]
jaredpar@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.

Markus Stoeger
6/29/2006 11:26:09 PM
[quoted text, click to view]

I dont know of another way than to call socket.Close() and catch the
resulting ObjectDisposedException on EndAccept.

hth,
rossu
6/30/2006 12:33:02 PM
listener.close

[quoted text, click to view]
Jared Parsons [MSFT]
6/30/2006 9:03:43 PM
Hello rossu,

[quoted text, click to view]

It seems like the option you're looking for here is a way to essentially
cancel the BeginAccept() call. Unfortunately that is not implemented for
the IAsyncResult pattern.

You're best option is to differentiate based on the exception when you call
EndAccept. If it's an ObjectDisposed exception that means that you called
Close on the listener socket someplace else. Otherwise there was a general
issue when someone attempted to connect to the socket.

--
Jared Parsons [MSFT]
jaredpar@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.

AddThis Social Bookmark Button