Close TCP/IP Listening Socket

  • Thread starter Thread starter Chad Dalton
  • Start date Start date
C

Chad Dalton

I am developing an asynchronous socket application and I
want the user to be able to start and stop the server.
Everything works fine when the server is started, but I
can't figure out how to close the listening socket. I've
tried listenersckt.shutdown and listenersckt.close, but I
receive an error that the socket is not connected. In
asynchronous sockets the listening socket will never by
connected. I have the StartListening Procedure running on
a background thread to prevent the user interface from
freezing up. I thought if I killed this thread that it
would destroy the listening socket, but it doesn't.
If I set the listenersckt = nothing, it still doesn't stop
the listening socket. Any ideas on how to put an
asynchronous socket in an unlistening state to prevent any
connections from being made to the server.
 
Socket.Close() is not working for you. If not, could you provide a simple
sample of what your doing.
 
William,

Socket.close worked, but I thought I tried that before. However, if no
clients are connected and I try to stop the server then I receive the
following error message.
"An unhandled exception of type System.Invalid Operation Exception
occured in system.dll (More info AcceptCallBack)".

I connect to the server when I stop the server to unblock the listener
thread so it will fall out of the connection processing loop. Here is
the code:

'Loop until the User Stops the Server
While Not gATMShuttingDown

'Set the event to nonsignaled state
allDone.Reset()

'Start an asynchronous TCP/IP Socket to listen for connections
listenerSckt.BeginAccept(New AsyncCallback(AddressOf
AcceptCallBack), listenerSckt)

'-----------------------------------------------------------'Wait until
a connection is made before continuing.
'-----------------------------------------------------------allDone.Wait
One()
End While
Catch ex As Exception
msgbox ex.tostring()
End Try
'Release resources to prevent any connections from being made

'Close socket
listenerSckt.Close()







Thanks for the help!!!!!

Chad
 
Can't really say from this sample Chad. If your just forcing a close, just
catch the error.
 
I've experienced a similar problem where if I have a socket connected via a certain port, and I call listen_socket.Close(), there will still be something listening on that socket. I don't know whether I receive an exception or not (I might be swallowing it unknowingly), but I'll check tonight when I get home

~~K
 
Chad,

I've seen this. Calling Close is the right thing to do, but you have to be
careful in your callback.

Disclaimer: I just caught the exception as part of shutdown but...

If you play around with the debugger you will notice that you get an error
code in the IAsyncResult passed into the callback. Alas, it is unusable,
being a member of the parent System.Net.LazyAsyncResult class which is not
accessible. Yuck.

You could set a flag before calling close which you would then check in the
callback, but this would seem to open up a race condition where a
"legitimate" callback could sneak in between.

Anyone have any thoughts for handling this gracefully other than catching
the exception?

-Jason
 
Guys,

I found the solution to closing the Listening TCP/IP Socket without
having an error occur. It seemed that the serverThread I created was
completing before my callBack code. In order to prevent this from
happening, I created a ManualReset event to use as a thread signal to
ensure that the callback code always completed before my serverThread
code completed. When the AcceptCallBack method completes, I set the
thread signal to set.

'Wait on signal from callback before closing socket.
callBackDone.waitone

'close listener socket since callback code has completed.
listenerSocket.close


Chad
 
Back
Top