[Visual C#] Whois域名查询协议实现

本文介绍了一个简单的C#程序,该程序通过TCP客户端连接到WHOIS服务器来查询域名信息。程序首先初始化TCP客户端并设置缓冲区大小,然后发送查询请求并接收返回的数据。
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using System.IO;
 5 using System.Net;
 6 using System.Net.Sockets;
 7 
 8 namespace ConsoleApplication1
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             // 参数一:待查询的域名
15             // 参数二:WHOIS服务器
16             // 参数三:WHOIS端口
17 
18             TcpClient tcpClient = new TcpClient();                                          // 初始化新的TCPCLIENT实例
19                 
20             tcpClient.Connect(args[1], Int32.Parse(args[2]));                               // 连接 Whois Server
21 
22             tcpClient.SendBufferSize = 1024;                                                // 设置发送缓冲大小
23 
24             tcpClient.ReceiveBufferSize = 2048;                                             // 设置接收缓冲大小
25 
26             byte[] sendByte = Encoding.Default.GetBytes(string.Format("{0}\n", args[0]));   // 转换发送请求的报文
27 
28             tcpClient.Client.Send(sendByte);                                                // 发送查询请求
29 
30             string strReceive = string.Empty;                                               // 初始化一个接收查询结果字符串
31 
32             byte[] receiveByte = new byte[2048];                                            // 初始化一接收数据数组
33 
34             while (tcpClient.Client.Receive(receiveByte) > 0)
35                 strReceive += Encoding.Default.GetString(receiveByte);                      // 接收查询结果
36 
37             strReceive = strReceive.Replace("\n""\r\n");                                  // 把Unix换行符转换为Windows换行符
38 
39             Console.WriteLine(strReceive);                                                  // 输出结果
40         }
41     }
42 }

转载于:https://www.cnblogs.com/briny/archive/2012/03/07/2382909.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值