How to Stop a Listening socket?

G

Guest

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
aborted. This should be trivial. What am I missing?
 
M

Markus Stoeger

rossu said:
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
aborted. This should be trivial. What am I missing?

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

hth,
Max
 
J

Jared Parsons [MSFT]

Hello rossu,
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 aborted. This should be trivial. What am I missing?

How are you stopping the Socket?
 
G

Guest

listener.close

Jared Parsons said:
Hello rossu,
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 aborted. This should be trivial. What am I missing?

How are you stopping the Socket?

--
Jared Parsons [MSFT]
(e-mail address removed)
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
 
J

Jared Parsons [MSFT]

Hello rossu,
listener.close

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top