Failed to Read From the Socket '127.0.0.1:11411'. Error: Connectionaborted

Dotnet logo

Dev Team blog

How Socket Fault Codes Depend on Runtime and Operating System

This post is the first function of a blog mail service series that covers different technical challenges that we had to resolve during the migration of the Rider backend process from Mono to .Net Cadre. By sharing our experiences, nosotros hope to assist out those who are in the same boat.
There's too much to share in one post, so we will brand this into a serial of posts. In this series:

  • How Socket Error Codes Depend on Runtime and Operating Arrangement
  • How Sorting Order Depends on Runtime and Operating Organisation
  • How ListSeparator Depends on Runtime and Operating System

Let'south dive in!

Sockets and error codes

Passenger consists of several processes that send messages to each other via sockets. To ensure the reliability of the whole application, information technology's of import to properly handle all the socket errors. In our codebase, nosotros had the post-obit code which was adopted from Mono Debugger Libs and helps us communicate with debugger processes:

protected virtual bool ShouldRetryConnection (Exception ex, int attemptNumber) {     var sx = ex equally SocketException;     if (sx != null) {         if (sx.ErrorCode == 10061) //connection refused             return true;     }     return false; }

In the case of a failed connection because of a "ConnectionRefused" mistake, we are retrying the connection attempt. It works fine with .Internet Framework and Mono. However, once we migrated to .Cyberspace Cadre, this method no longer correctly detects the "connectedness refused" situation on Linux and macOS. If we open the SocketException documentation, we volition learn that this class has iii dissimilar backdrop with mistake codes:

  • SocketError SocketErrorCode: Gets the error code that is associated with this exception.
  • int ErrorCode: Gets the mistake code that is associated with this exception.
  • int NativeErrorCode: Gets the Win32 fault code associated with this exception.

What's the deviation betwixt these properties? Should nosotros await different values on different runtimes or different operating systems? Which one should nosotros apply in product? Why do we have issues with ShouldRetryConnection on .NET Cadre? Permit's effigy it all out!

Digging into the problem

Let'due south start with the following program, which prints error lawmaking belongings values for SocketError.ConnectionRefused:

var se = new SocketException((int) SocketError.ConnectionRefused); Console.WriteLine((int)se.SocketErrorCode); Console.WriteLine(se.ErrorCode); Console.WriteLine(se.NativeErrorCode);

If we run it on Windows, we will become the same value on .Cyberspace Framework, Mono, and .Cyberspace Core:

SocketErrorCode ErrorCode NativeErrorCode
.NET Framework 10061 10061 10061
Mono 10061 10061 10061
.Cyberspace Core 10061 10061 10061

10061 corresponds to the code of the connection refused socket mistake code in Windows (too known as WSAECONNREFUSED).
Now let'south run the same plan on Linux:

SocketErrorCode ErrorCode NativeErrorCode
Mono 10061 10061 10061
.NET Core 10061 111 111

Equally you can see, Mono returns Windows-compatible error codes. The situation with .NET Cadre is different: it returns a Windows-compatible value for SocketErrorCode (10061) and a Linux-like value for ErrorCode and NativeErrorCode (111).
Finally, allow'southward check macOS:

SocketErrorCode ErrorCode NativeErrorCode
Mono 10061 10061 10061
.Internet Core 10061 61 61

Here, Mono is completely Windows-compatible once again, but .Internet Core returns 61 for ErrorCode and NativeErrorCode.
In the IBM Knowledge Center, we can discover a few more values for the connexion refused mistake lawmaking from the Unix world (also known as ECONNREFUSED):

  • AIX: 79
  • HP-UX: 239
  • Solaris: 146

For a amend understanding of what'due south going on, let'southward bank check out the source code of all the properties.

SocketErrorCode

SocketException.SocketErrorCode returns a value from the SocketError enum. The numerical values of the enum elements are the same on all the runtimes (see its implementation in .NET Framework, .Net Core 3.1.3, and Mono 6.8.0.105):

public enum SocketError {     SocketError = -1, // 0xFFFFFFFF     Success = 0,     OperationAborted = 995, // 0x000003E3     IOPending = 997, // 0x000003E5     Interrupted = 10004, // 0x00002714     AccessDenied = 10013, // 0x0000271D     Fault = 10014, // 0x0000271E     InvalidArgument = 10022, // 0x00002726     TooManyOpenSockets = 10024, // 0x00002728     WouldBlock = 10035, // 0x00002733     InProgress = 10036, // 0x00002734     AlreadyInProgress = 10037, // 0x00002735     NotSocket = 10038, // 0x00002736     DestinationAddressRequired = 10039, // 0x00002737     MessageSize = 10040, // 0x00002738     ProtocolType = 10041, // 0x00002739     ProtocolOption = 10042, // 0x0000273A     ProtocolNotSupported = 10043, // 0x0000273B     SocketNotSupported = 10044, // 0x0000273C     OperationNotSupported = 10045, // 0x0000273D     ProtocolFamilyNotSupported = 10046, // 0x0000273E     AddressFamilyNotSupported = 10047, // 0x0000273F     AddressAlreadyInUse = 10048, // 0x00002740     AddressNotAvailable = 10049, // 0x00002741     NetworkDown = 10050, // 0x00002742     NetworkUnreachable = 10051, // 0x00002743     NetworkReset = 10052, // 0x00002744     ConnectionAborted = 10053, // 0x00002745     ConnectionReset = 10054, // 0x00002746     NoBufferSpaceAvailable = 10055, // 0x00002747     IsConnected = 10056, // 0x00002748     NotConnected = 10057, // 0x00002749     Shutdown = 10058, // 0x0000274A     TimedOut = 10060, // 0x0000274C     ConnectionRefused = 10061, // 0x0000274D     HostDown = 10064, // 0x00002750     HostUnreachable = 10065, // 0x00002751     ProcessLimit = 10067, // 0x00002753     SystemNotReady = 10091, // 0x0000276B     VersionNotSupported = 10092, // 0x0000276C     NotInitialized = 10093, // 0x0000276D     Disconnecting = 10101, // 0x00002775     TypeNotFound = 10109, // 0x0000277D     HostNotFound = 11001, // 0x00002AF9     TryAgain = 11002, // 0x00002AFA     NoRecovery = 11003, // 0x00002AFB     NoData = 11004, // 0x00002AFC }

These values represent to the Windows Sockets Error Codes.

NativeErrorCode

In .Cyberspace Framework and Mono, SocketErrorCode and NativeErrorCode e'er have the same values:

public SocketError SocketErrorCode {     //     // the base class returns the HResult with this property     // we need the Win32 Fault Code, hence the override.     //     go {         render (SocketError)NativeErrorCode;     } }

In .NET Core, the native code is calculated in the constructor (see SocketException.cs#L20):

public SocketException(int errorCode) : this((SocketError)errorCode) // ... internal SocketException(SocketError socketError) : base of operations(GetNativeErrorForSocketError(socketError))

The Windows implementation of GetNativeErrorForSocketError is trivial (see SocketException.Windows.cs):

private static int GetNativeErrorForSocketError(SocketError fault) {     // SocketError values map direct to Win32 error codes     return (int)error; }

The Unix implementation is more complicated (see SocketException.Unix.cs):

private static int GetNativeErrorForSocketError(SocketError error) {     int nativeErr = (int)error;     if (error != SocketError.SocketError)     {         Interop.Error interopErr;          // If an interop error was non constitute, so don't invoke Info().RawErrno as that volition fail with assert.         if (SocketErrorPal.TryGetNativeErrorForSocketError(error, out interopErr))         {             nativeErr = interopErr.Info().RawErrno;         }     }      return nativeErr; }

TryGetNativeErrorForSocketError should convert SocketError to the native Unix mistake code.
Unfortunately, at that place exists no unequivocal mapping between Windows and Unix error codes. Every bit such, the .NET team decided to create a Lexicon that maps error codes in the best possible way (see SocketErrorPal.Unix.cs):

individual const int NativeErrorToSocketErrorCount = 42; private const int SocketErrorToNativeErrorCount = forty;  // No Interop.Errors are included for the post-obit SocketErrors, as there'southward no good mapping: // - SocketError.NoRecovery // - SocketError.NotInitialized // - SocketError.ProcessLimit // - SocketError.SocketError // - SocketError.SystemNotReady // - SocketError.TypeNotFound // - SocketError.VersionNotSupported  private static readonly Dictionary<Interop.Error, SocketError> s_nativeErrorToSocketError = new Lexicon<Interop.Error, SocketError>(NativeErrorToSocketErrorCount) {     { Interop.Mistake.EACCES, SocketError.AccessDenied },     { Interop.Error.EADDRINUSE, SocketError.AddressAlreadyInUse },     { Interop.Mistake.EADDRNOTAVAIL, SocketError.AddressNotAvailable },     { Interop.Mistake.EAFNOSUPPORT, SocketError.AddressFamilyNotSupported },     { Interop.Mistake.EAGAIN, SocketError.WouldBlock },     { Interop.Error.EALREADY, SocketError.AlreadyInProgress },     { Interop.Error.EBADF, SocketError.OperationAborted },     { Interop.Fault.ECANCELED, SocketError.OperationAborted },     { Interop.Error.ECONNABORTED, SocketError.ConnectionAborted },     { Interop.Mistake.ECONNREFUSED, SocketError.ConnectionRefused },     { Interop.Mistake.ECONNRESET, SocketError.ConnectionReset },     { Interop.Error.EDESTADDRREQ, SocketError.DestinationAddressRequired },     { Interop.Fault.EFAULT, SocketError.Fault },     { Interop.Mistake.EHOSTDOWN, SocketError.HostDown },     { Interop.Mistake.ENXIO, SocketError.HostNotFound }, // not perfect, only closest match available     { Interop.Error.EHOSTUNREACH, SocketError.HostUnreachable },     { Interop.Error.EINPROGRESS, SocketError.InProgress },     { Interop.Error.EINTR, SocketError.Interrupted },     { Interop.Mistake.EINVAL, SocketError.InvalidArgument },     { Interop.Error.EISCONN, SocketError.IsConnected },     { Interop.Error.EMFILE, SocketError.TooManyOpenSockets },     { Interop.Mistake.EMSGSIZE, SocketError.MessageSize },     { Interop.Error.ENETDOWN, SocketError.NetworkDown },     { Interop.Error.ENETRESET, SocketError.NetworkReset },     { Interop.Error.ENETUNREACH, SocketError.NetworkUnreachable },     { Interop.Error.ENFILE, SocketError.TooManyOpenSockets },     { Interop.Fault.ENOBUFS, SocketError.NoBufferSpaceAvailable },     { Interop.Error.ENODATA, SocketError.NoData },     { Interop.Error.ENOENT, SocketError.AddressNotAvailable },     { Interop.Fault.ENOPROTOOPT, SocketError.ProtocolOption },     { Interop.Error.ENOTCONN, SocketError.NotConnected },     { Interop.Error.ENOTSOCK, SocketError.NotSocket },     { Interop.Error.ENOTSUP, SocketError.OperationNotSupported },     { Interop.Mistake.EPERM, SocketError.AccessDenied },     { Interop.Error.EPIPE, SocketError.Shutdown },     { Interop.Mistake.EPFNOSUPPORT, SocketError.ProtocolFamilyNotSupported },     { Interop.Error.EPROTONOSUPPORT, SocketError.ProtocolNotSupported },     { Interop.Error.EPROTOTYPE, SocketError.ProtocolType },     { Interop.Fault.ESOCKTNOSUPPORT, SocketError.SocketNotSupported },     { Interop.Fault.ESHUTDOWN, SocketError.Disconnecting },     { Interop.Mistake.SUCCESS, SocketError.Success },     { Interop.Error.ETIMEDOUT, SocketError.TimedOut }, };  individual static readonly Dictionary<SocketError, Interop.Error> s_socketErrorToNativeError = new Dictionary<SocketError, Interop.Error>(SocketErrorToNativeErrorCount) {     // This is *mostly* an inverse mapping of s_nativeErrorToSocketError.  Yet, some options have multiple mappings and thus     // can't be inverted direct.  Other options don't take a mapping from native to SocketError, but when presented with a SocketError,     // nosotros desire to provide the closest relevant Error possible, due east.g. EINPROGRESS maps to SocketError.InProgress, and vice versa, but     // SocketError.IOPending too maps closest to EINPROGRESS.  As such, roundtripping won't necessarily provide the original value 100% of the time,     // simply information technology's the best we tin do given the mismatch betwixt Interop.Error and SocketError.      { SocketError.AccessDenied, Interop.Error.EACCES}, // could also have been EPERM     { SocketError.AddressAlreadyInUse, Interop.Fault.EADDRINUSE  },     { SocketError.AddressNotAvailable, Interop.Error.EADDRNOTAVAIL },     { SocketError.AddressFamilyNotSupported, Interop.Mistake.EAFNOSUPPORT  },     { SocketError.AlreadyInProgress, Interop.Fault.EALREADY },     { SocketError.ConnectionAborted, Interop.Fault.ECONNABORTED },     { SocketError.ConnectionRefused, Interop.Error.ECONNREFUSED },     { SocketError.ConnectionReset, Interop.Fault.ECONNRESET },     { SocketError.DestinationAddressRequired, Interop.Error.EDESTADDRREQ },     { SocketError.Disconnecting, Interop.Error.ESHUTDOWN },     { SocketError.Error, Interop.Error.EFAULT },     { SocketError.HostDown, Interop.Error.EHOSTDOWN },     { SocketError.HostNotFound, Interop.Mistake.EHOSTNOTFOUND },     { SocketError.HostUnreachable, Interop.Error.EHOSTUNREACH },     { SocketError.InProgress, Interop.Error.EINPROGRESS },     { SocketError.Interrupted, Interop.Error.EINTR },     { SocketError.InvalidArgument, Interop.Fault.EINVAL },     { SocketError.IOPending, Interop.Error.EINPROGRESS },     { SocketError.IsConnected, Interop.Error.EISCONN },     { SocketError.MessageSize, Interop.Error.EMSGSIZE },     { SocketError.NetworkDown, Interop.Error.ENETDOWN },     { SocketError.NetworkReset, Interop.Error.ENETRESET },     { SocketError.NetworkUnreachable, Interop.Error.ENETUNREACH },     { SocketError.NoBufferSpaceAvailable, Interop.Error.ENOBUFS },     { SocketError.NoData, Interop.Error.ENODATA },     { SocketError.NotConnected, Interop.Fault.ENOTCONN },     { SocketError.NotSocket, Interop.Fault.ENOTSOCK },     { SocketError.OperationAborted, Interop.Error.ECANCELED },     { SocketError.OperationNotSupported, Interop.Error.ENOTSUP },     { SocketError.ProtocolFamilyNotSupported, Interop.Fault.EPFNOSUPPORT },     { SocketError.ProtocolNotSupported, Interop.Error.EPROTONOSUPPORT },     { SocketError.ProtocolOption, Interop.Error.ENOPROTOOPT },     { SocketError.ProtocolType, Interop.Error.EPROTOTYPE },     { SocketError.Shutdown, Interop.Error.EPIPE },     { SocketError.SocketNotSupported, Interop.Fault.ESOCKTNOSUPPORT },     { SocketError.Success, Interop.Error.SUCCESS },     { SocketError.TimedOut, Interop.Error.ETIMEDOUT },     { SocketError.TooManyOpenSockets, Interop.Error.ENFILE }, // could likewise have been EMFILE     { SocketError.TryAgain, Interop.Error.EAGAIN }, // not a perfect mapping, merely better than zilch     { SocketError.WouldBlock, Interop.Mistake.EAGAIN  }, };  internal static bool TryGetNativeErrorForSocketError(SocketError error, out Interop.Error errno) {     return s_socketErrorToNativeError.TryGetValue(error, out errno); }          

Once nosotros take an instance of Interop.Error, we phone call interopErr.Info().RawErrno. The implementation of RawErrno can exist found in Interop.Errors.cs:

internal int RawErrno {     get { return _rawErrno == -1 ? (_rawErrno = Interop.Sys.ConvertErrorPalToPlatform(_error)) : _rawErrno; } }  [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_ConvertErrorPalToPlatform")] internal static extern int ConvertErrorPalToPlatform(Fault error);

Here we are jumping to the native function SystemNative_ConvertErrorPalToPlatform that maps Error to the native integer lawmaking that is defined in errno.h. Y'all tin become all the values using the errno util. Hither is a typical output on Linux:

$ errno -ls EPERM ane Performance non permitted ENOENT 2 No such file or directory ESRCH three No such process EINTR 4 Interrupted system call EIO 5 Input/output mistake ENXIO six No such device or address E2BIG 7 Argument list also long ENOEXEC viii Exec format fault EBADF 9 Bad file descriptor ECHILD ten No kid processes EAGAIN eleven Resources temporarily unavailable ENOMEM 12 Cannot classify memory EACCES 13 Permission denied EFAULT 14 Bad address ENOTBLK fifteen Block device required EBUSY 16 Device or resources decorated EEXIST 17 File exists EXDEV 18 Invalid cross-device link ENODEV 19 No such device ENOTDIR 20 Not a directory EISDIR 21 Is a directory EINVAL 22 Invalid argument ENFILE 23 Too many open files in system EMFILE 24 Too many open files ENOTTY 25 Inappropriate ioctl for device ETXTBSY 26 Text file busy EFBIG 27 File besides large ENOSPC 28 No space left on device ESPIPE 29 Illegal seek EROFS 30 Read-simply file organisation EMLINK 31 Too many links EPIPE 32 Broken piping EDOM 33 Numerical argument out of domain ERANGE 34 Numerical result out of range EDEADLK 35 Resource deadlock avoided ENAMETOOLONG 36 File name too long ENOLCK 37 No locks available ENOSYS 38 Part not implemented ENOTEMPTY 39 Directory not empty ELOOP 40 Too many levels of symbolic links EWOULDBLOCK 11 Resource temporarily unavailable ENOMSG 42 No message of desired blazon EIDRM 43 Identifier removed ECHRNG 44 Aqueduct number out of range EL2NSYNC 45 Level two not synchronized EL3HLT 46 Level iii halted EL3RST 47 Level 3 reset ELNRNG 48 Link number out of range EUNATCH 49 Protocol driver not attached ENOCSI 50 No CSI structure available EL2HLT 51 Level 2 halted EBADE 52 Invalid exchange EBADR 53 Invalid request descriptor EXFULL 54 Exchange total ENOANO 55 No anode EBADRQC 56 Invalid request code EBADSLT 57 Invalid slot EDEADLOCK 35 Resource deadlock avoided EBFONT 59 Bad font file format ENOSTR threescore Device non a stream ENODATA 61 No data available ETIME 62 Timer expired ENOSR 63 Out of streams resources ENONET 64 Machine is not on the network ENOPKG 65 Package not installed EREMOTE 66 Object is remote ENOLINK 67 Link has been severed EADV 68 Annunciate error ESRMNT 69 Srmount error ECOMM 70 Advice error on send EPROTO 71 Protocol mistake EMULTIHOP 72 Multihop attempted EDOTDOT 73 RFS specific mistake EBADMSG 74 Bad message EOVERFLOW 75 Value too big for defined data blazon ENOTUNIQ 76 Proper noun non unique on network EBADFD 77 File descriptor in bad state EREMCHG 78 Remote address inverse ELIBACC 79 Can non access a needed shared library ELIBBAD lxxx Accessing a corrupted shared library ELIBSCN 81 .lib section in a.out corrupted ELIBMAX 82 Attempting to link in likewise many shared libraries ELIBEXEC 83 Cannot exec a shared library directly EILSEQ 84 Invalid or incomplete multibyte or wide character ERESTART 85 Interrupted organisation call should exist restarted ESTRPIPE 86 Streams piping error EUSERS 87 As well many users ENOTSOCK 88 Socket functioning on non-socket EDESTADDRREQ 89 Destination address required EMSGSIZE ninety Message likewise long EPROTOTYPE 91 Protocol wrong blazon for socket ENOPROTOOPT 92 Protocol not bachelor EPROTONOSUPPORT 93 Protocol not supported ESOCKTNOSUPPORT 94 Socket blazon non supported EOPNOTSUPP 95 Performance not supported EPFNOSUPPORT 96 Protocol family unit not supported EAFNOSUPPORT 97 Address family not supported past protocol EADDRINUSE 98 Address already in use EADDRNOTAVAIL 99 Cannot assign requested address ENETDOWN 100 Network is down ENETUNREACH 101 Network is unreachable ENETRESET 102 Network dropped connexion on reset ECONNABORTED 103 Software caused connection abort ECONNRESET 104 Connexion reset by peer ENOBUFS 105 No buffer space available EISCONN 106 Transport endpoint is already continued ENOTCONN 107 Ship endpoint is not connected ESHUTDOWN 108 Cannot transport after send endpoint shutdown ETOOMANYREFS 109 Too many references: cannot splice ETIMEDOUT 110 Connexion timed out ECONNREFUSED 111 Connection refused EHOSTDOWN 112 Host is down EHOSTUNREACH 113 No route to host EALREADY 114 Functioning already in progress EINPROGRESS 115 Operation now in progress ESTALE 116 Stale file handle EUCLEAN 117 Structure needs cleaning ENOTNAM 118 Not a XENIX named type file ENAVAIL 119 No XENIX semaphores available EISNAM 120 Is a named type file EREMOTEIO 121 Remote I/O error EDQUOT 122 Disk quota exceeded ENOMEDIUM 123 No medium found EMEDIUMTYPE 124 Incorrect medium type ECANCELED 125 Operation canceled ENOKEY 126 Required fundamental non available EKEYEXPIRED 127 Primal has expired EKEYREVOKED 128 Key has been revoked EKEYREJECTED 129 Central was rejected by service EOWNERDEAD 130 Owner died ENOTRECOVERABLE 131 Country non recoverable ERFKILL 132 Operation not possible due to RF-impale EHWPOISON 133 Memory page has hardware mistake ENOTSUP 95 Operation non supported

Annotation that errno may be not bachelor by default in your Linux distro. For instance, on Debian, y'all should telephone call sudo apt-get install moreutils to get this utility.
Hither is a typical output on macOS:

$ errno -ls EPERM 1 Operation not permitted ENOENT ii No such file or directory ESRCH 3 No such procedure EINTR four Interrupted system phone call EIO 5 Input/output error ENXIO six Device non configured E2BIG 7 Argument listing too long ENOEXEC 8 Exec format error EBADF 9 Bad file descriptor ECHILD 10 No kid processes EDEADLK 11 Resources deadlock avoided ENOMEM 12 Cannot allocate retention EACCES 13 Permission denied EFAULT 14 Bad address ENOTBLK 15 Block device required EBUSY 16 Resource busy EEXIST 17 File exists EXDEV xviii Cross-device link ENODEV 19 Operation not supported past device ENOTDIR 20 Not a directory EISDIR 21 Is a directory EINVAL 22 Invalid argument ENFILE 23 Too many open up files in organization EMFILE 24 Too many open files ENOTTY 25 Inappropriate ioctl for device ETXTBSY 26 Text file decorated EFBIG 27 File too large ENOSPC 28 No space left on device ESPIPE 29 Illegal seek EROFS 30 Read-only file system EMLINK 31 As well many links EPIPE 32 Cleaved pipe EDOM 33 Numerical statement out of domain ERANGE 34 Result too large EAGAIN 35 Resource temporarily unavailable EWOULDBLOCK 35 Resources temporarily unavailable EINPROGRESS 36 Functioning now in progress EALREADY 37 Operation already in progress ENOTSOCK 38 Socket performance on non-socket EDESTADDRREQ 39 Destination address required EMSGSIZE 40 Message too long EPROTOTYPE 41 Protocol wrong type for socket ENOPROTOOPT 42 Protocol not available EPROTONOSUPPORT 43 Protocol not supported ESOCKTNOSUPPORT 44 Socket type not supported ENOTSUP 45 Operation non supported EPFNOSUPPORT 46 Protocol family not supported EAFNOSUPPORT 47 Address family not supported past protocol family EADDRINUSE 48 Address already in utilise EADDRNOTAVAIL 49 Tin can`t assign requested address ENETDOWN fifty Network is down ENETUNREACH 51 Network is unreachable ENETRESET 52 Network dropped connection on reset ECONNABORTED 53 Software caused connection arrest ECONNRESET 54 Connection reset past peer ENOBUFS 55 No buffer infinite available EISCONN 56 Socket is already connected ENOTCONN 57 Socket is non connected ESHUTDOWN 58 Can`t send after socket shutdown ETOOMANYREFS 59 Besides many references: tin can`t splice ETIMEDOUT threescore Functioning timed out ECONNREFUSED 61 Connection refused ELOOP 62 Too many levels of symbolic links ENAMETOOLONG 63 File name likewise long EHOSTDOWN 64 Host is down EHOSTUNREACH 65 No route to host ENOTEMPTY 66 Directory non empty EPROCLIM 67 Too many processes EUSERS 68 As well many users EDQUOT 69 Disc quota exceeded ESTALE 70 Stale NFS file handle EREMOTE 71 Too many levels of remote in path EBADRPC 72 RPC struct is bad ERPCMISMATCH 73 RPC version incorrect EPROGUNAVAIL 74 RPC prog. non avail EPROGMISMATCH 75 Program version wrong EPROCUNAVAIL 76 Bad procedure for programme ENOLCK 77 No locks available ENOSYS 78 Part not implemented EFTYPE 79 Inappropriate file blazon or format EAUTH 80 Hallmark error ENEEDAUTH 81 Need authenticator EPWROFF 82 Device power is off EDEVERR 83 Device error EOVERFLOW 84 Value too large to be stored in data type EBADEXEC 85 Bad executable (or shared library) EBADARCH 86 Bad CPU type in executable ESHLIBVERS 87 Shared library version mismatch EBADMACHO 88 Malformed Mach-o file ECANCELED 89 Performance canceled EIDRM 90 Identifier removed ENOMSG 91 No message of desired type EILSEQ 92 Illegal byte sequence ENOATTR 93 Attribute non plant EBADMSG 94 Bad message EMULTIHOP 95 EMULTIHOP (Reserved) ENODATA 96 No message available on STREAM ENOLINK 97 ENOLINK (Reserved) ENOSR 98 No STREAM resources ENOSTR 99 Not a STREAM EPROTO 100 Protocol error ETIME 101 STREAM ioctl timeout EOPNOTSUPP 102 Functioning not supported on socket ENOPOLICY 103 Policy not found ENOTRECOVERABLE 104 State not recoverable EOWNERDEAD 105 Previous owner died EQFULL 106 Interface output queue is full ELAST 106 Interface output queue is full

Hooray! We've finished our fascinating journey into the internals of socket error codes. Now you know where .Cyberspace is getting the native error code for each SocketException from!

ErrorCode

The ErrorCode property is the most boring ane, equally it always returns NativeErrorCode.
.Internet Framework, Mono half dozen.8.0.105:

public override int ErrorCode {     //     // the base grade returns the HResult with this belongings     // we need the Win32 Error Code, hence the override.     //     go {         return NativeErrorCode;     } }

In .NET Core 3.1.3:

public override int ErrorCode => base.NativeErrorCode;

Writing cross-platform socket error handling

Circling dorsum to the original method we started this post with, we rewrote ShouldRetryConnection as follows:

protected virtual bool ShouldRetryConnection(Exception ex) {     if (ex is SocketException sx)         return sx.SocketErrorCode == SocketError.ConnectionRefused;     render false; }

In that location was a lot of work involved in tracking down the error code to check against, only in the cease, our lawmaking is much more readable at present. Adding to that, this method is now also completely cross-platform, and works correctly on whatever runtime.

Overview of the native fault codes

In some situations, you may want to have a table with native error codes on different operating systems. We tin get these values with the following code snippet:

var allErrors = Enum.GetValues(typeof(SocketError)).Cast<SocketError>().ToList(); var maxNameWidth = allErrors.Select(x => ten.ToString().Length).Max(); foreach (var socketError in allErrors) {     var name = socketError.ToString().PadRight(maxNameWidth);     var code = new SocketException((int) socketError).NativeErrorCode.ToString().PadLeft(seven);     Panel.WriteLine($TEXT$quot;| {name} | {code} |"); }

Nosotros executed this program on Windows, Linux, and macOS. Hither are the aggregated results:

SocketError Windows Linux macOS
Success 0 0 0
OperationAborted 995 125 89
IOPending 997 115 36
Interrupted 10004 four four
AccessDenied 10013 13 13
Fault 10014 14 14
InvalidArgument 10022 22 22
TooManyOpenSockets 10024 23 23
WouldBlock 10035 11 35
InProgress 10036 115 36
AlreadyInProgress 10037 114 37
NotSocket 10038 88 38
DestinationAddressRequired 10039 89 39
MessageSize 10040 90 40
ProtocolType 10041 91 41
ProtocolOption 10042 92 42
ProtocolNotSupported 10043 93 43
SocketNotSupported 10044 94 44
OperationNotSupported 10045 95 45
ProtocolFamilyNotSupported 10046 96 46
AddressFamilyNotSupported 10047 97 47
AddressAlreadyInUse 10048 98 48
AddressNotAvailable 10049 99 49
NetworkDown 10050 100 l
NetworkUnreachable 10051 101 51
NetworkReset 10052 102 52
ConnectionAborted 10053 103 53
ConnectionReset 10054 104 54
NoBufferSpaceAvailable 10055 105 55
IsConnected 10056 106 56
NotConnected 10057 107 57
Shutdown 10058 32 32
TimedOut 10060 110 threescore
ConnectionRefused 10061 111 61
HostDown 10064 112 64
HostUnreachable 10065 113 65
ProcessLimit 10067 10067 10067
SystemNotReady 10091 10091 10091
VersionNotSupported 10092 10092 10092
NotInitialized 10093 10093 10093
Disconnecting 10101 108 58
TypeNotFound 10109 10109 10109
HostNotFound 11001 -131073 -131073
TryAgain 11002 eleven 35
NoRecovery 11003 11003 11003
NoData 11004 61 96
SocketError -1 -1 -1

This table may exist useful if you lot work with native socket error codes.

Summary

From this investigation, nosotros've learned the following:

  • SocketException.SocketErrorCode returns a value from the SocketError enum. The numerical values of the enum elements always correspond to the Windows socket error codes.
  • SocketException.ErrorCode always returns SocketException.NativeErrorCode.
  • SocketException.NativeErrorCode on .Net Framework and Mono always corresponds to the Windows error codes (fifty-fifty if y'all are using Mono on Unix). On .NET Core, SocketException.NativeErrorCode equals the corresponding native mistake code from the current operating organisation.

SocketErrors-Blog
A few practical recommendations:

  • If you want to write portable code, always use SocketException.SocketErrorCode and compare it with the values of SocketError. Never utilize raw numerical error codes.
  • If y'all want to become the native fault code on .NET Core (e.one thousand., for passing to another native program), use SocketException.NativeErrorCode. Think that different Unix-based operating systems (e.1000., Linux, macOS, Solaris) have different native lawmaking sets. Y'all can get the exact values of the native fault codes by using the errno command.

References

  • Microsoft Docs: Windows Sockets Mistake Codes
  • IBM Knowledge Centre: TCP/IP mistake codes
  • MariaDB: Operating Organization Error Codes
  • gnu.org: Error Codes
  • Stackoverflow: Identical Error Codes

Discover more than

earnestdecong.blogspot.com

Source: https://blog.jetbrains.com/dotnet/2020/04/27/socket-error-codes-depend-runtime-operating-system/

0 Response to "Failed to Read From the Socket '127.0.0.1:11411'. Error: Connectionaborted"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel