如何创建数据流以使用 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/