0

我正在编写一个应用程序来检索给定网络上所有计算机的名称和 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地址。

4

3 回答 3

1

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

于 2015-09-01T05:15:53.367 回答
0

在这里查看这篇文章

SocketException:地址与请求的协议不兼容

这解释了问题的原因。

于 2012-07-05T14:17:59.570 回答
0

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

于 2016-03-22T09:39:59.020 回答