| 記事リスト |
| [5141] DNS / C# |
| □投稿者/ himajin100000 @-(2009/07/04(Sat) 14:02:03) |
| [引用] |
/* System.Net.Dnsは特定のDNSサーバを指定してクエリを発行することが出来ません。ので(勉強も兼ねて他サイトのライブラリを利用せず)UDPで自作してみようと思ったのだがReceiveで止まる。 */
using System;
namespace test2 { class Program { public static void Main(string[] args) { System.Net.Sockets.UdpClient c = new System.Net.Sockets.UdpClient(); System.Net.IPEndPoint ep = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("192.168.1.1"),53); c.Connect(ep); System.IO.MemoryStream ms = new System.IO.MemoryStream(); ms.Write(new byte[]{0x01,0x00},0,2); /* ID */ ms.Write(new byte[]{0x00,0x00},0,2); /* FLAG */ ms.Write(new byte[]{0x01,0x00},0,2); /* QCOUNT */ ms.Write(new byte[]{0x00,0x00},0,2); /* ACOUNT */ ms.Write(new byte[]{0x00,0x00},0,2); /* NSCOUNT */ ms.Write(new byte[]{0x00,0x00},0,2); /* ARCOUNT */ System.Collections.Generic.List<String> x = new System.Collections.Generic.List<String>("microsoft.com".Split(new char[]{'.'})); x.ForEach( delegate(string s){ ms.Write(BitConverter.GetBytes(System.Text.Encoding.ASCII.GetBytes(s).Length),0,2); ms.Write(System.Text.Encoding.ASCII.GetBytes(s),0,System.Text.Encoding.ASCII.GetBytes(s).Length); } ); ms.Write(new byte[]{0x00,0x00},0,2); /* FIN */ ms.Write(new byte[]{0x01,0x00},0,2); /* Qtype */ ms.Write(new byte[]{0x01,0x00},0,2); /* QClass */ new System.Collections.Generic.List<byte>(ms.ToArray()).ForEach(delegate(byte b){ System.Console.WriteLine(b);}); c.Send(ms.ToArray(),ms.ToArray().Length); System.Diagnostics.Debug.Print(c.Receive(ref ep).Length.ToString()); c.Close();
} } } |
|
| [5160]
Re: DNS / C# |
□投稿者/ エイジ(管理人)-(2009/07/08(Wed) 23:47:53)
|
| Res1 | [引用] |
こんばんは。 返事が遅くなりましてすいません。
えっと、7/8付のBlogでも書きましたが、C#わからないんですよね…。 ん〜〜〜〜。 たぶんReciveで止まるのは、送信したQueryが正しくないのでエラーではじかれちゃってるからじゃないでしょうか。 送信したデータグラムをキャプチャして確かめるとよいですよ。
んん〜。 たぶん、たぶんですが。 >ms.Write(BitConverter.GetBytes(System.Text.Encoding.ASCII.GetBytes(s).Length),0,2); ココが原因じゃないかな、と。 ラベルの先頭につけるラベル長は、2バイトじゃなくて、1バイトですので。最後の引数の2はバイト数ですよね。
あまりお役にたてなくてすいません。 今後ともよろしくお願いします。 |
| 記事リスト |
|
|