.NET Library Documentation

SocketException Class

Represents an error that occurs during a socket operation. This exception is thrown by the Socket class when a socket-related error occurs.

Inheritance

Summary

The SocketException class provides detailed information about the network error that occurred. It includes a SocketErrorCode property, which is a value that indicates the specific error code returned by the operating system. Understanding these error codes is crucial for diagnosing and resolving network connectivity issues in your applications.

Remarks

When a SocketException is thrown, it often indicates a problem with the underlying network infrastructure, such as a network unreachable error, a connection timed out, or a protocol violation. The SocketErrorCode property provides a standardized way to interpret these errors across different platforms.

SocketErrorCode Property

The SocketErrorCode property of the SocketException class returns a SocketError enumeration value. This enumeration defines common socket error codes, such as:

You can check this property in a catch block to determine the specific cause of the socket error.

Example Usage

The following example demonstrates how to catch a SocketException and check its error code.


using System;
using System.Net;
using System.Net.Sockets;

public class SocketExample
{
    public static void Main(string[] args)
    {
        try
        {
            // Attempt to connect to a non-existent server
            using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                IPAddress ipAddress = IPAddress.Parse("192.0.2.1"); // Example non-routable IP
                IPEndPoint remoteEP = new IPEndPoint(ipAddress, 12345); // Arbitrary port

                socket.Connect(remoteEP);
            }
        }
        catch (SocketException socketEx)
        {
            Console.WriteLine($"SocketException caught: {socketEx.Message}");
            Console.WriteLine($"SocketErrorCode: {socketEx.SocketErrorCode}");

            // Handle specific error codes
            switch (socketEx.SocketErrorCode)
            {
                case SocketError.ConnectionRefused:
                    Console.WriteLine("Error: Connection refused by the remote host.");
                    break;
                case SocketError.TimedOut:
                    Console.WriteLine("Error: The connection attempt timed out.");
                    break;
                case SocketError.NetworkUnreachable:
                    Console.WriteLine("Error: The network is unreachable.");
                    break;
                default:
                    Console.WriteLine($"An unhandled socket error occurred: {socketEx.SocketErrorCode}");
                    break;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An unexpected error occurred: {ex.Message}");
        }
    }
}
            

Related Topics