try { mySocket.Connect(remoteEP); } catch (SocketException e) { SocketError error = e.SocketErrorCode.GetStatus(); if (error == SocketError.ConnectionRefused || error == SocketError.TimedOut) { Console.WriteLine("Connection failed due to {0} error.", error); } }
try { byte[] data = Encoding.ASCII.GetBytes("Hello, world!"); mySocket.Send(data); } catch (SocketException e) { SocketError error = e.SocketErrorCode.GetStatus(); if (error == SocketError.Disconnecting || error == SocketError.ConnectionReset) { Console.WriteLine("Error occurred while sending data: {0}", error); } }In this example, the GetStatus method is called on the SocketErrorCode property of a SocketException object that is thrown by the Send method. The status of the error code is then checked to determine if there was a problem disconnecting or if the connection was reset. Package library: The SocketError class is part of the System.Net.Sockets namespace in the .NET Framework. Therefore, no external package or library is needed to use the GetStatus method.