如何从SocketException获取当前套接字?
我有所有可用套接字的列表,如果该套接字不再可用,我想将其从列表中删除.
try
{
/* Socket stuff */
}
catch (SocketException se)
{
if (ex.ErrorCode == (int)SocketError.ConnectionAborted)
{
// Remove socket from list
ConnectionAborted( currentSocket );
}
}
我已经检查了MSDN,但找不到任何东西.
因此,ConnectionAborted方法应从列表中删除发生问题的套接字,以便不再检查新数据.
解决方法:
当网络发生错误时,Socket和Dns类将引发SocketException.
这意味着Socket对象的方法之一会引发SocketException,因此您已经知道它的源代码,并且代码应如下所示:
Socket currentSocket;
try
{
/* Socket stuff with currentSocket */
}
catch (SocketException se)
{
if (ex.ErrorCode == (int)SocketError.ConnectionAborted)
{
//Remove socket from list
ConnectionAborted( currentSocket ); // At his point currentSocket is in scope.
}
}