SocketException Class
Namespace: System.Net.Sockets
Assembly: System.Net.Sockets.dll
⚠️
Represents an error that occurred when using a socket.
Syntax
public class SocketException : Win32Exception
Inheritance
Object → Exception → SystemException → ExternalException → Win32Exception → SocketException
Remarks
The SocketException class represents an error that occurred when attempting to perform a socket operation. This exception is thrown by methods in the System.Net.Sockets namespace. The ErrorCode property of the SocketException class contains the Windows Sockets API error code that was returned by the operating system.
You can use the ErrorCode property to determine the specific socket error. For a list of common socket error codes, refer to the documentation for the SocketError enumeration.
Constructors
SocketException()- Initializes a new instance of the
SocketExceptionclass with default values. SocketException(int error)- Initializes a new instance of the
SocketExceptionclass with a specified Windows Sockets API error code. error- The Windows Sockets API error code.
SocketException(string message)- Initializes a new instance of the
SocketExceptionclass with a specified error message. message- A message that describes the error.
SocketException(string message, Exception innerException)- Initializes a new instance of the
SocketExceptionclass with a specified error message and a reference to the inner exception that is the cause of this exception. message- The error message that explains the reason for the exception.
innerException- The exception that is the cause of the current exception. If
innerExceptionis notnull, the current exception is raised in acatchblock that handles the inner exception.
Properties
ErrorCode- Gets the error code returned by the operating system.
Message- Gets a message that describes the current exception.
NativeErrorCode- Gets the native error code from the operating system.
StackTrace- Gets a string representation of the immediate frame in the call stack.
InnerException- Gets the exception that is the cause of the current exception.
Examples
The following example demonstrates how to catch a SocketException and display the error code.
using System;
using System.Net;
using System.Net.Sockets;
public class Example
{
public static void Main(string[] args)
{
try
{
// Attempt a socket operation that is likely to fail
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(IPAddress.Loopback, 1); // Port 1 is usually closed
}
catch (SocketException e)
{
Console.WriteLine($"SocketException caught: {e.Message}");
Console.WriteLine($"Error Code: {e.ErrorCode}");
Console.WriteLine($"Native Error Code: {e.NativeErrorCode}");
// You can map the error code to SocketError enum for more context
if (Enum.IsDefined(typeof(SocketError), e.ErrorCode))
{
SocketError socketError = (SocketError)e.ErrorCode;
Console.WriteLine($"Socket Error: {socketError}");
}
}
catch (Exception e)
{
Console.WriteLine($"An unexpected exception occurred: {e.Message}");
}
}
}