【问题标题】:Getting no results because SocketException occurred in System.dll没有结果,因为 System.dll 中发生了 SocketException
【发布时间】:2012-07-05 13:40:20
【问题描述】:

我正在编写一个应用程序来检索给定网络上所有计算机的名称和 IP 地址。这是为了在远程连接到计算机时使用技术支持。我仍在开发并仅在我自己的网络上进行测试,但是当我运行它时,我没有得到任何结果,并且这会显示在输出中。

A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll

我的代码如下:

public List<NetworkComputer> getComputers( string domain )
        {
            List<NetworkComputer> computers = new List<NetworkComputer>();

            DirectoryEntry entry = new DirectoryEntry( domain );
            DirectorySearcher searcher = new DirectorySearcher( entry );
            searcher.Filter = ("(objectClass=computer)");
            searcher.SizeLimit = int.MaxValue;
            searcher.PageSize = int.MaxValue;

            foreach( SearchResult result in searcher.FindAll() )
            {
                if( result.GetDirectoryEntry().Name.StartsWith( "CN=" ) )
                {
                    IPAddress ipAddress = null;

                    ipAddress = Dns.GetHostAddresses( result.GetDirectoryEntry().Name.Remove( 0, "CN=".Length ) )[0];

                    computers.Add( new NetworkComputer( result.GetDirectoryEntry().Name.Remove( 0, "CN=".Length ), ipAddress ) );
                }
            }

            return computers;
        }
    }
}

如果我在不尝试获取 IP 地址的情况下运行它,我会得到一个没有问题的所有计算机的列表。是 IP 地址给我带来了问题。

【问题讨论】:

标签: c# networking ip


【解决方案1】:

如果您将连接字符串设置为 localhost,只需更改为“127.0.0.1”,错误就会被删除。

【讨论】:

  • 为什么会这样?
【解决方案2】:

在这里查看这篇文章

SocketException: address incompatible with requested protocol

这解释了问题的原因。

【讨论】:

    【解决方案3】:

    在我的例子中,这个 exe 的另一个实例已经准备好运行。当我从任务管理器中关闭它时,它就解决了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 2019-07-18
      • 2017-03-08
      • 1970-01-01
      相关资源
      最近更新 更多