如何使用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);发布于 2015-10-30 17:56:54
如果问题只是“从C#中的DNS获取域名IP”,那么简单的答案是
var res = Dns.GetHostEntry("google.com").AddressList;此语句返回具有解析域名的IP地址的字符串数组。
如果问题更复杂“使用TCP/IP协议从C#中的DNS获取域名IP”,请查看here。
https://stackoverflow.com/questions/33441867
复制相似问题