Reputation: 1399
I used a component for creating socket connection. Now, I have a client and server that can connect to each other by the component. but my problem is it: When some error happening, I got a msg like this:
System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it
I want to know is any relation between this code (0x80004005) and Winsock Error Codes in MSDN WebSite ? what is this code mean? is it display the value error code?! or something like this?
actually I want to get the related value like 10061
but I dont know how I can get it by above string value. Thanks for any helping.
Upvotes: 4
Views: 8193
Reputation: 2751
The ErrorCode property of the exception object contains the socket error code. The list of error codes are defined here.
The error in your case is WSAECONNREFUSED 10061
BTW, you have to catch the SocketException and not the general Exception to get the error code.
try
{
}
catch (System.Net.Sockets.SocketException sockEx)
{
int errorCode = sockEx.ErrorCode;
}
If, however, you want to get the native error code, you can use sockEx.NativeErrorCode
instead.
Upvotes: 6
Reputation: 121799
If you structure your code to catch a C# exception, then the exception should have the error text.
EXAMPLE:
try
{
...
}
catch (Exception ex)
MessaageBox.show(ex.toString());
}
Failing all else, you can always PInvoke the old Win32 FormatMessage:
http://bobobobo.wordpress.com/2009/02/02/getting-winsock-error-messages-in-string-format/
And there's always sockets.com (from my 16-bit WinSock2 programming days):
Upvotes: 1