I have writen the following souce code :
m_Socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
m_Socket.Blocking = false;
try
{
m_Socket.Connect(ipEndPoint);
}
catch (SocketException e)
{
int timePace = 500000;
try
{
bool retCode = m_Socket.Poll(timePace,
SelectMode.SelectWrite); //Step1 , check the server status.
m_Context.IsLastPollSuccess = retCode;
//System.Console.WriteLine( retCode );
if (retCode == true)
{
m_Context.LastSocketErrorCode = 0;
m_Context.IsSocketConnected = true;
}
else
{
throw new
MyAppException(MyAppExceptionRes.GetString(MyAppEx ceptionRes.MyAppException_16_SystemCallError,
new object[] { "Socket.Poll", e.ErrorCode
}));
//Step2 poll method return false, throw an
general error message.
}
// set socket option
//......
}
catch (SocketException e1)
{ //Step3 according the errorcode to get the error
message.
if (e1.ErrorCode == 10049 || e1.ErrorCode == 10051 )
// WSAEADDRNOTAVAIL=10049 , WSAENETUNREACH=10051
{
throw new
MyAppException(MyAppExceptionRes.GetString(MyAppEx ceptionRes.MyAppException_17_HostAddressErrorInHos tFile));
}
else if (e1.ErrorCode == 10060) // WSAETIMEDOUT
=10060
{
throw new
MyAppException(MyAppExceptionRes.GetString(MyAppEx ceptionRes.MyAppException_14_CannotCommunicateWith Server));
}
else if (e1.ErrorCode == 10048) // WSAEADDRINUSE
=10048
{
throw new
MyAppException(MyAppExceptionRes.GetString(MyAppEx ceptionRes.MyAppException_18_ServerHasBeenUsedAddr ess));
}
else if (e1.ErrorCode == 10061) //
WSAECONNREFUSED=10061
{
throw new
MyAppException(MyAppExceptionRes.GetString(MyAppEx ceptionRes.MyAppException_19_ServerRefusedConnecti on));
}
else if (e1.ErrorCode == 10050) // WSAENETDOWN
=10050
{
throw new
MyAppException(MyAppExceptionRes.GetString(MyAppEx ceptionRes.MyAppException_12_GeneralNetworkError)) ;
}
else if (e1.ErrorCode == 10055) // WSAENOBUFS =10055
{
throw new
MyAppException(MyAppExceptionRes.GetString(MyAppEx ceptionRes.MyAppException_13_InsufficientMemory));
}
else
{
throw new
MyAppException(MyAppExceptionRes.GetString(MyAppEx ceptionRes.MyAppException_16_SystemCallError,
new object[] { "Socket.Poll", e1.ErrorCode
}));
}
}
accor ding to the "user-fixable errors", the url is :
http://www.sockets.com/a_c2.htm#UserFixableErrors
I am try to use the socket.poll method to check the server status after
the socket.connect method, and it will throw out exception with a
specified errormessage according to the server status.
but, the program has never arrived at the step 3.
The poll method always return false without exception , even if I input
one error IP and Port, i draw out the network.
I do not know what's wrong with my source code, are there some bugs in
my code?
In addition, who can tell me when the poll method will throw exception?
would you please to tell me how to create such a condition to invoke
the exception?
anyway, thanks to all of you .