c# - 从 DNS 获取 IP

标签 c# tcp dns udp

如何创建数据流以使用 TCP 查找特定域的 IP? 搜索论坛并尝试这样的事情:

client = new UdpClient();
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("8.8.8.8"), 53);
client.Connect(ep);
String query = "host -T google.com";
byte[] myByte = System.Text.ASCIIEncoding.Default.GetBytes(query);
client.Send(myByte, myByte.Length);
var receivedData = client.Receive(ref ep);

我不明白如何为 DNS 服务器创建正确的消息。如果存在类似于 HTTP 方式的东西,那就太好了。

更新:这就是您的做法!

    client = new UdpClient();
    IPEndPoint ep = new IPEndPoint(IPAddress.Parse("8.8.8.8"), 53);
    client.Connect(ep);

    //dynamic dns
    String host1 = "vg.no";
    byte[] hostnameLength = new byte[1];
    byte[] hostdomainLength = new byte[1];


    byte[] tranactionID1 = { 0x46, 0x62 };
    byte[] queryType1 = { 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    byte[] hostname = System.Text.ASCIIEncoding.Default.GetBytes(host1.Split('.')[0]);
    hostnameLength[0] = (byte)hostname.Length;
    byte[] hostdomain = System.Text.ASCIIEncoding.Default.GetBytes(host1.Split('.')[1]);
    hostdomainLength[0] = (byte)hostdomain.Length;
    byte[] queryEnd = {0x00, 0x00, 0x01, 0x00, 0x01};
    byte[] dnsQueryString = tranactionID1.Concat(queryType1).Concat(hostnameLength).Concat(hostname).Concat(hostdomainLength).Concat(hostdomain).Concat(queryEnd).ToArray();
    client.Send(dnsQueryString, dnsQueryString.Length);

最佳答案

如果问题只是“在 C# 中从 DNS 获取域的 IP”,那么简单的答案是

var res = Dns.GetHostEntry("google.com").AddressList;

此语句返回一个字符串数组,其中包含解析域名的 IP 地址。

如果问题更复杂“使用 TCP/IP 协议(protocol)在 C# 中从 DNS 获取域的 IP”,请查看 here

关于c# - 从 DNS 获取 IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33441867/

相关文章:

c# - 这是为我的屏幕抓取器缓存页面的糟糕方法吗?

c# - 使用 Streamreader 的输入不会在 StreamWriter 中给出相同的输出

请求前的 HTTP 响应

c# - 检查类是否不是类型的更易读的方法?

c# - 锁定私有(private)静态对象

linux - 如何同时接受 TCP 连接 <ip :port> in many threads?

html - 返回html代码

networking - 什么是单播 DNS,它与其他类型的 DNS 有何不同?

dns - 为什么更新域的名称服务器需要这么长时间?

asp.net - 如何向 Web 服务器的域管理员隐藏我存储在数据库中的数据?