SocketException Class
System.Net.Sockets
Represents an error that occurs during a socket operation.
Inheritance:
Object > Exception > SystemException > SocketException
Constructors
- SocketException()
- SocketException(Int32 errorCode)
- SocketException(String message)
- SocketException(String message, Exception innerException)
Properties
- ErrorCode:
Int32- Gets the error code that is associated with the exception. - Message:
String- Gets a message that describes the current exception. (Inherited fromException) - InnerException:
Exception- Gets an instance ofExceptionthat is the cause of the current exception. (Inherited fromException) - StackTrace:
String- Gets a string representation of the immediate frames of a call stack. (Inherited fromException) - HelpLink:
String- Gets or sets a link to the help file associated with this exception. (Inherited fromException) - HResult:
Int32- Gets or sets HRESULT values. (Inherited fromException)
Methods
- ToString():
String- Returns a string representation of the current exception. (Inherited fromException) - GetObjectData(SerializationInfo info, StreamingContext context):
Void- Sets the SerializationInfo object with information about the exception. (Inherited fromException)
SocketException()
Initializes a new instance of the SocketException class.
SocketException(Int32 errorCode)
Initializes a new instance of the SocketException class with the specified error code.
Parameters
errorCode: AnInt32that specifies the error code for the exception.
SocketException(String message)
Initializes a new instance of the SocketException class with a specified error message.
Parameters
message: AStringdescribing the error that occurred.
SocketException(String message, Exception innerException)
Initializes a new instance of the SocketException class with a specified error message and a reference to the inner exception that is the cause of this exception.
Parameters
message: The error message that explains the reason for the exception.innerException: The exception that is the cause of the current exception. IfinnerExceptionis notnull, the current exception is raised in acatchblock that handles the inner exception.
Remarks
The SocketException class is used to report errors that occur during socket operations. The ErrorCode property provides specific information about the error that occurred. This property can be used to retrieve a system-defined error code for the socket error.
Common socket error codes include:
10001:WSA_INVALID_HANDLE- An invalid handle was used.10004:WSA_NOT_ENOUGH_MEMORY- Not enough memory available to complete the operation.10013:WSA_ACCESS_DENIED- An attempt was made to access a socket in a way forbidden by its access permissions.10048:WSAEADDRINUSE- Only one usage of each socket address (protocol/network address/port) is normally permitted.10054:WSAECONNRESET- An existing connection was forcibly closed by the remote host.10061:WSAECONNREFUSED- No connection could be made because the target machine actively refused it.
Example
using System;
using System.Net;
using System.Net.Sockets;
public class SocketExceptionExample
{
public static void Main(string[] args)
{
try
{
// Attempt to connect to a non-existent server to trigger an exception
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
IPAddress ipAddress = IPAddress.Parse("192.168.1.254"); // Example of a potentially unreachable IP
int port = 12345; // Example of a closed port
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
socket.Connect(remoteEP);
}
}
catch (SocketException se)
{
Console.WriteLine($"SocketException caught: {se.Message}");
Console.WriteLine($"Error Code: {se.ErrorCode}");
// You can look up the error code in Windows Sockets API documentation
// or use a helper method to translate it to a string.
}
catch (Exception e)
{
Console.WriteLine($"An unexpected error occurred: {e.Message}");
}
}
}