Problem with AcceptCallback closing server socket

  • Thread starter Thread starter David
  • Start date Start date
D

David

Having:Socket mysocket = new Socket
(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);

I cannot execute mysocket.Close() after executing
mysocket.BeginAccept(new AsyncCallback(ServerAcceptConn)..

The system complains giving a hint "AcceptCallback"

What should I execute before closing to not have this
error message?
Thanks.
 
David said:
Having:Socket mysocket = new Socket
(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);

I cannot execute mysocket.Close() after executing
mysocket.BeginAccept(new AsyncCallback(ServerAcceptConn)..

The system complains giving a hint "AcceptCallback"

What should I execute before closing to not have this
error message?
David -

I made a response to this earlier, but I don't see it posted, so
either something went wrong, or you will see two responses from me.
Sorry about that.

When the socket object is closed with an outstanding BeginAccept()
method, the AsyncCallback method will be automatically fired, and the
EndAccept() method will throw an ObjectDisposedException. You need to
catch that using a try-catch block, and handle it accordingly. Hope
this helps solve your problem.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0471433012.html
 
Back
Top