请问:如何用C#实现,ping功能。

如果谁能提供源代码的话,非常感激!
---------------------------------------------------------------

use Process
http://radio.weblogs.com/0111551/stories/2002/10/11/runASecondConsoleAppAndReadItsOutput.html

Ping In C#
http://www.csharphelp.com/archives2/archive296.html

http://www.c-sharpcorner.com/network/ping.asp
---------------------------------------------------------------

ping程序的C#实现
--------------------------------------------------------------------------------

namespace Cornfield
{
using System;
using System.Net;
using System.Net.Sockets;

class MyPing
{

public static void Main(string[] argv)
{
if(argv.Length==1 ¦ ¦ argv.Length==2)
PingHost(argv);
else
{
Console.WriteLine("Invalid Command.");
Console.WriteLine("Usage : 1. MyPing

  1<hostname>.") ;   
  2Console.WriteLine(" 2. MyPing <hostname> <client>.");   
  3}   
  4  
  5}   
  6  
  7public static void PingHost(string[] hostclient)   
  8{   
  9//初始化Socket套接字   
 10//三个参数分别为:   
 11// 1。解析地址的地址模式,较常用的为AddressFamily.InterNetwork,即IPv4地址。   
 12// 2。Socket套接字类型,一般为SocketType.Raw原始类型。   
 13// 3。网络协议类型,这里Ping用的是Internet控制报文协议ProtocolType.Icmp.   
 14Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);   
 15  
 16IPHostEntry hostInfo;   
 17try   
 18{   
 19//解析主机IP入口   
 20hostInfo = Dns.GetHostByName(hostclient[0]);   
 21}   
 22catch(Exception)   
 23{   
 24//解析主机名错误。   
 25Console.WriteLine("Unknown host {0}.",hostclient[0]);   
 26return ;   
 27}   
 28// 取主机0号端口   
 29EndPoint hostPoint= (EndPoint)new IPEndPoint(hostInfo.AddressList[0], 0);   
 30  
 31IPHostEntry clientInfo;   
 32try   
 33{   
 34if (hostclient.Length==2)   
 35clientInfo =Dns.GetHostByName(hostclient[1]);   
 36else   
 37clientInfo = Dns.GetHostByName(Dns.GetHostName());   
 38}   
 39catch(Exception)   
 40{   
 41//解析主机名错误。   
 42Console.WriteLine("Unknown client {1}.",hostclient[1]);   
 43return ;   
 44}   
 45// 取客户机0号端口   
 46//********************************注意******************************   
 47//这里当然可以设置为本地机或其它主机的IP,那样报文将返回到该机IP端口,   
 48//实际上这是黑客们惯用的手段之一,即利用报文协议来攻击远程主机   
 49//便是利用这样的原理,加上多个无限次的报文攻击即可时远程主机瘫痪   
 50//对于将本程序利用于非法的攻击活动,笔者概不负责!!!!!!!!!!!!!!!!   
 51//******************************************************************   
 52EndPoint clientPoint = (EndPoint)new IPEndPoint(clientInfo.AddressList[0], 0);   
 53  
 54//设置ICMP报文   
 55int DataSize = 32; // ICMP数据包大小;   
 56int PacketSize = DataSize + 8;//总报文长度   
 57const int ICMP_ECHO = 8;   
 58IcmpPacket packet = new IcmpPacket(ICMP_ECHO,0,0,45,0,DataSize);   
 59  
 60Byte [] Buffer = new Byte[ PacketSize ];   
 61int index=packet.ConvertToByte(Buffer);   
 62//报文出错   
 63if( index != PacketSize)   
 64{   
 65Console.WriteLine("There is something wrong with Packet Data !");   
 66return ;   
 67}   
 68  
 69int cksum_buffer_length =(int)Math.Ceiling( ((Double)index)/ 2);   
 70  
 71UInt16 [] cksum_buffer = new UInt16[cksum_buffer_length];   
 72  
 73int icmp_header_buffer_index = 0;   
 74for( int i = 0; i &lt; cksum_buffer_length; i++ )   
 75{   
 76//将两个byte转化为一个UInt16   
 77cksum_buffer[i] = BitConverter.ToUInt16(Buffer,icmp_header_buffer_index);   
 78icmp_header_buffer_index += 2;   
 79}   
 80  
 81//将校验和保存至报文里   
 82packet.CheckSum =IcmpPacket.SumOfCheck(cksum_buffer);   
 83// 保存校验和后,再次将报文转化为数据包   
 84Byte [] SendData = new Byte[ PacketSize ];   
 85index= packet.ConvertToByte(SendData);   
 86//报文出错   
 87if( index != PacketSize)   
 88{   
 89Console.WriteLine("There is something wrong with Packet Data !");   
 90return ;   
 91}   
 92  
 93  
 94int nBytes=0;   
 95//系统计时开始   
 96int startTime = Environment.TickCount;   
 97//发送数据包   
 98if ((nBytes = socket.SendTo(SendData, PacketSize, SocketFlags.None,(EndPoint) hostPoint)) == -1)   
 99{   
100Console.WriteLine("Socket can not send packet");   
101}   
102  
103  
104Byte [] ReceiveData = new Byte[256]; //接收数据   
105nBytes = 0;   
106  
107int timeout=0 ;   
108int timeConsume=0;   
109  
110while(true)   
111{   
112  
113\---------------------------------------------------------------   
114  
115nBytes = socket.ReceiveFrom(ReceiveData, 256,SocketFlags.None,ref clientPoint);   
116if (nBytes == -1)   
117{   
118Console.WriteLine("Host not Responding") ;   
119break;   
120}   
121else if(nBytes&gt;0)   
122{   
123timeConsume = System.Environment.TickCount - startTime;   
124Console.WriteLine("Reply from "+hostPoint.ToString()+" in "   
125+timeConsume+"MS :Bytes Received"+nBytes);   
126break;   
127}   
128timeConsume=Environment.TickCount - startTime;   
129if(timeout&gt;1000)   
130{   
131Console.WriteLine("Time Out") ;   
132break;   
133}   
134}   
135  
136//关闭套接字   
137socket.Close();   
138}   
139}   
140  
141  
142  
143public class IcmpPacket   
144{   
145private Byte _type; // 报文类型   
146private Byte _subCode; // 字代码类型   
147private UInt16 _checkSum; // 报文校验和   
148private UInt16 _identifier; // 识别符   
149private UInt16 _sequenceNumber; // 序列号   
150private Byte [] _data; //数据包   
151  
152///***********************************************************   
153///初始化报文   
154///***********************************************************   
155public IcmpPacket(Byte type,Byte subCode,UInt16 checkSum,UInt16 identifier,UInt16 sequenceNumber,int dataSize)   
156{   
157_type=type;   
158_subCode=subCode;   
159_checkSum=checkSum;   
160_identifier=identifier;   
161_sequenceNumber=sequenceNumber;   
162_data=new Byte[dataSize];   
163for (int i = 0; i &lt; dataSize; i++)   
164{   
165_data[i] = (byte)'#';   
166}   
167}   
168public UInt16 CheckSum   
169{   
170get   
171{   
172return _checkSum;   
173}   
174set   
175{   
176_checkSum=value;   
177}   
178}   
179  
180///************************************************************   
181///将整个ICMP报文信息和数据转化为Byte数据包   
182///************************************************************   
183public int Convert</client></hostname></hostname>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus