Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get socket exception value

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.

like image 649
Elahe Avatar asked Sep 04 '25 16:09

Elahe


1 Answers

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.

like image 125
bobbyalex Avatar answered Sep 07 '25 16:09

bobbyalex