locked
Avoiding exception when a socket is closed RRS feed

  • Question

  • I have a basic server application with async-sockets for sending/receiving data over TCP. When a connection is closed, it often throws an exception in a socket-related function such as Socket.BeginReceive, Socket.EndReceive, EndSend, etc. In order to avoid the exception, I added a check for if the socket is connected by calling Socket.IsConnected. However, I still get exceptions in my code (An existing connection was forcibly closed by the remote host).

    Is there a way to avoid getting exceptions when a socket is closed by checking some properties?

    • Moved by Lisa Zhu Tuesday, December 11, 2012 9:52 AM network related (From:Visual C# General)
    Sunday, December 9, 2012 9:43 PM

Answers

  • It appears by using the SocketError as out parameter in the socket functions I could avoid all of the exceptions. Thanks :D
    • Marked as answer by martin_mine Thursday, December 13, 2012 2:53 PM
    Thursday, December 13, 2012 2:53 PM

All replies

  • Hi martin,

    Welcome to MSDN Forum.

    From your description, I ‘d like to move this post to  the most related forum for better support.

    There are more  experts in this aspect, so you may have more luck getting answers.

    Thanks for your understanding.

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 11, 2012 9:52 AM
  • The errors are probably caused by the connection closing and no acknowledgement of the connection closing is occuring.  You are using a TCP connection and every message that is sent is confirmed on the far end by sending a response.  If the server doesn't get an ack an exception will occur. 

    But not every close event will get acknoledged.  If a lap top computer connects to your server and then the ethernet cable is disconnected from the server the server will still think the connection is active.  When the server attempts to close the connection at a later time there is no client connected so you don't get the acknowledgement and an exception occurs.

    No matter what you do to solve the problem will cause an exception.  For example you could enable the keeps alive poperty which periodically tests an client is still connected by sending a datagram to the client with no data in the packet.  When the client shuts down the keep alive will fail and cause an exception.   Then you can close the connection in software, but the closing will again cause another exception because the close command also expects an acknowledgement from the client.  So to prevent one type of exception from occuring you are creating two other exceptions.


    jdweng

    Tuesday, December 11, 2012 2:52 PM
  • It appears by using the SocketError as out parameter in the socket functions I could avoid all of the exceptions. Thanks :D
    • Marked as answer by martin_mine Thursday, December 13, 2012 2:53 PM
    Thursday, December 13, 2012 2:53 PM