.NET Socket.Connect 将主机名解析为 IP 地址

发布于 2024-10-01 17:21:55 字数 278 浏览 1 评论 0原文

我正在查看一些代码中的 .NET 套接字连接函数。 http://msdn.microsoft.com/en-us/library/d7ew360f.aspx 该函数接受一个字符串作为参数,表示远程主机的名称。

我的问题是,套接字如何获取该远程主机的 IP 地址?它是否执行 DNS 查找或者是否可以转到 Windows 主机文件? (Windows XP)

I'm looking at the .NET socket connect function in some code. http://msdn.microsoft.com/en-us/library/d7ew360f.aspx This function takes in a string as an argument that represents the name of the remote host.

My question is, how does the socket then go about getting the IP address of this remote host? Does it perform a DNS lookup or can it go to the Windows hosts file? (Windows XP)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

关于从前 2024-10-08 17:21:55

使用Dns.GetHostAddresses(host);

实际代码片段如下:

public void Connect(string host, int port){
    // Checking parameters etc, removed.

     IPAddress[] addresses = Dns.GetHostAddresses(host);
     Connect(addresses,port);
}

因此,具有主机名的构造函数只需调用具有 IP 地址的构造函数。

请参阅 http://msdn.microsoft.com/en- us/library/system.net.dns.gethostaddresses.aspx 有关 Dns.GetHostAddresses() 的文档。

Using Dns.GetHostAddresses(host);.

A fragment of the actual code is as follows:

public void Connect(string host, int port){
    // Checking parameters etc, removed.

     IPAddress[] addresses = Dns.GetHostAddresses(host);
     Connect(addresses,port);
}

So, the constructor with the hostname simply calls the constructor with the IP addresses.

See http://msdn.microsoft.com/en-us/library/system.net.dns.gethostaddresses.aspx for documentation about Dns.GetHostAddresses().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文