简体   繁体   English

从SocketException获取实际的套接字

[英]Get the actual socket from SocketException

How do I get the current socket from a SocketException? 如何从SocketException获取当前套接字?

I have a list of all available sockets and if the socket is no longer available I would like to remove it from the list. 我有一个所有可用套接字的列表,如果套接字不再可用,我想从列表中删除它。

try
{
    /* Socket stuff */
}
catch (SocketException se)
{
    if (ex.ErrorCode == (int)SocketError.ConnectionAborted)
    {
        // Remove socket from list
        ConnectionAborted( currentSocket );
    }
}

I have checked MSDN , but couldn't find anything. 我检查了MSDN ,但找不到任何东西。

So the method ConnectionAborted should remove the socket where the problem occurs from the list so it no longer check for new data. 因此, ConnectionAborted方法应该从列表中删除出现问题的套接字,以便它不再检查新数据。

A SocketException is thrown by the Socket and Dns classes when an error occurs with the network. 当网络发生错误时,Socket和Dns类抛出SocketException。

This means that one of the methods of Socket objects throws SocketException , therefore you already know its source and the code shpould be something like this: 这意味着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.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM