Question

How do I get the current socket from a SocketException?

I have a list of all available sockets and if the socket is no longer available I would like to remove it from the list.

try
{
    /* Socket stuff */
}
catch (SocketException se)
{
    if (ex.ErrorCode == (int)SocketError.ConnectionAborted)
    {
        // Remove socket from list
        ConnectionAborted( currentSocket );
    }
}

I have checked MSDN, but couldn't find anything.

So the method ConnectionAborted should remove the socket where the problem occurs from the list so it no longer check for new data.

Was it helpful?

Solution

A SocketException is thrown by the Socket and Dns classes when an error occurs with the network.

This means that one of the methods of Socket objects throws SocketException, therefore you already know its source and the code shpould be something like this:

Socket currentSocket;

try
{
    /* Socket stuff with currentSocket */
}
catch (SocketException se)
{
    if (ex.ErrorCode == (int)SocketError.ConnectionAborted)
    {
                    //Remove socket from list
        ConnectionAborted( currentSocket ); // At his point currentSocket is in scope.
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top