SocketException
Namespace: System.Net
Assembly: System.Net.Primitives.dll
Inheritance: Exception
Derived types: None.
Represents an error that occurred when performing a synchronous or asynchronous socket operation.
Remarks
The SocketException class provides specific error codes and messages that are returned by the operating system's socket APIs. When a socket operation fails, a SocketException is thrown, and you can examine its ErrorCode property to determine the specific cause of the error.
The error codes are typically the same as the Winsock error codes (WSA*) on Windows and the POSIX error codes on Unix-like systems.
Commonly Encountered Error Codes
Here are some of the most frequent error codes you might encounter:
| Error Code | Symbolic Name | Description |
|---|---|---|
| 10004 | WSAEINTR |
A blocking socket operation was interrupted by a call to WSACancelBlockingCall. |
| 10009 | WSAEBADF |
A file descriptor supplied to a socket function is not valid. |
| 10013 | WSAEACCES |
An attempt was made to access a socket with an operation that is not permitted (e.g., trying to bind an privileged port without administrator rights). |
| 10014 | WSAEFAULT |
Bad address. The system detected an invalid pointer address supplied by the operating system. |
| 10022 | WSAEINVAL |
An invalid argument was supplied to one of the socket functions. |
| 10024 | WSAEMFILE |
Too many open sockets. |
| 10035 | WSAEWOULDBLOCK |
The operation would block. This is normally returned by nonblocking socket operations. |
| 10036 | WSAEINPROGRESS |
A blocking operation is currently in progress. |
| 10037 | WSAEALREADY |
A nonblocking socket is connected. |
| 10038 | WSAENOTSOCK |
An operation was attempted on something that is not a socket. |
| 10048 | WSAEADDRINUSE |
A specified address is already in use. |
| 10049 | WSAEADDRNOTAVAIL |
A requested address was not valid. |
| 10050 | WSAENETDOWN |
The network subsystem has failed. |
| 10051 | WSAENETUNREACH |
A socket operation attempted to use an unreachable network. |
| 10053 | WSAECONNABORTED |
An established connection was aborted by the software in your host machine. |
| 10054 | WSAECONNRESET |
An existing connection was forcibly closed by the remote host. |
| 10055 | WSAENOBUFS |
A socket operation could not be performed because the system is out of buffer space. |
| 10056 | WSAEISCONN |
A previous connection attempt is already in progress. |
| 10057 | WSAENOTCONN |
A request to send or receive data was disallowed because the socket is not connected. |
| 10058 | WSAESHUTDOWN |
Cannot send after transport endpoint shutdown. |
| 10060 | WSAETIMEDOUT |
Connection timed out. |
| 10061 | WSAECONNREFUSED |
Connection refused. |
| 10064 | WSAEHOST_DOWN |
A socket operation sent to a host that is down. |
| 10065 | WSAEHOST_UNREACHABLE |
A socket operation attempted to use an unreachable host. |
| 10067 | WSAELOOP |
Too many levels of symbolic links. |
| 11001 | WSAHOST_NOT_FOUND |
Host not found. |
| 11002 | WSATRY_AGAIN |
This is a non-recoverable error and should be treated as an unknown error. |
| 11003 | WSANO_RECOVERY |
This is a non-recoverable error and should be treated as an unknown error. |
| 11004 | WSANO_DATA |
Valid name, no data record of requested type. |
Example
The following example demonstrates how to catch a SocketException and display its error code and message.
// Example of catching SocketException
try
{
// Attempt a socket operation that might fail, e.g., connecting to a closed port
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Loopback, 12345); // Assuming port 12345 is not open
socket.Connect(remoteEP);
}
}
catch (SocketException ex)
{
// Output the error code and message
Console.WriteLine($"SocketException caught: ErrorCode = {ex.ErrorCode}, Message = {ex.Message}");
// You can then map the error code to a known symbolic name or take specific actions
}
catch (Exception ex)
{
// Handle other potential exceptions
Console.WriteLine($"An unexpected exception occurred: {ex.Message}");
}