I have writen the following souce code :
m_Socket = new Socket(AddressF amily.InterNetw ork,
SocketType.Stre am, ProtocolType.Tc p);
m_Socket.Blocki ng = false;
try
{
m_Socket.Connec t(ipEndPoint);
}
catch (SocketExceptio n e)
{
int timePace = 500000;
try
{
bool retCode = m_Socket.Poll(t imePace,
SelectMode.Sele ctWrite); //Step1 , check the server status.
m_Context.IsLas tPollSuccess = retCode;
//System.Console. WriteLine( retCode );
if (retCode == true)
{
m_Context.LastS ocketErrorCode = 0;
m_Context.IsSoc ketConnected = true;
}
else
{
throw new
MyAppException( MyAppExceptionR es.GetString(My AppExceptionRes .MyAppException _16_SystemCallE rror,
new object[] { "Socket.Pol l", e.ErrorCode
}));
//Step2 poll method return false, throw an
general error message.
}
// set socket option
//......
}
catch (SocketExceptio n e1)
{ //Step3 according the errorcode to get the error
message.
if (e1.ErrorCode == 10049 || e1.ErrorCode == 10051 )
// WSAEADDRNOTAVAI L=10049 , WSAENETUNREACH= 10051
{
throw new
MyAppException( MyAppExceptionR es.GetString(My AppExceptionRes .MyAppException _17_HostAddress ErrorInHostFile ));
}
else if (e1.ErrorCode == 10060) // WSAETIMEDOUT
=10060
{
throw new
MyAppException( MyAppExceptionR es.GetString(My AppExceptionRes .MyAppException _14_CannotCommu nicateWithServe r));
}
else if (e1.ErrorCode == 10048) // WSAEADDRINUSE
=10048
{
throw new
MyAppException( MyAppExceptionR es.GetString(My AppExceptionRes .MyAppException _18_ServerHasBe enUsedAddress)) ;
}
else if (e1.ErrorCode == 10061) //
WSAECONNREFUSED =10061
{
throw new
MyAppException( MyAppExceptionR es.GetString(My AppExceptionRes .MyAppException _19_ServerRefus edConnection));
}
else if (e1.ErrorCode == 10050) // WSAENETDOWN
=10050
{
throw new
MyAppException( MyAppExceptionR es.GetString(My AppExceptionRes .MyAppException _12_GeneralNetw orkError));
}
else if (e1.ErrorCode == 10055) // WSAENOBUFS =10055
{
throw new
MyAppException( MyAppExceptionR es.GetString(My AppExceptionRes .MyAppException _13_Insufficien tMemory));
}
else
{
throw new
MyAppException( MyAppExceptionR es.GetString(My AppExceptionRes .MyAppException _16_SystemCallE rror,
new object[] { "Socket.Pol l", 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 .