现在的位置: 首页 > 综合 > 正文

在C#中实现Ping

2013年04月25日 ⁄ 综合 ⁄ 共 3237字 ⁄ 字号 评论关闭
using System;<br /> using System.Drawing;<br /> using System.Collections;<br /> using System.Data;<br /> using System.Net;<br /> using System.Net.Sockets;</p> <p>/// Ping类<br /> public class Ping<br /> {<br /> //声明常量<br /> const int SOCKET_ERROR = -1;<br /> const int ICMP_ECHO = 8;</p> <p>// 程序入口<br /> public static void Main()<br /> {<br /> Ping p = new Ping();<br /> Console.WriteLine("请输入要 Ping 的IP或者主机名字:");<br /> string MyUrl = Console.ReadLine();<br /> Console.WriteLine("正在 Ping " + MyUrl + " ……");<br /> Console.Write(p.PingHost(MyUrl));<br /> }</p> <p>public string PingHost(string host)<br /> {<br /> // 声明 IPHostEntry<br /> IPHostEntry serverHE, fromHE;<br /> int nBytes = 0;<br /> int dwStart = 0, dwStop = 0;</p> <p> //初始化ICMP的Socket<br /> Socket socket =<br /> new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);<br /> socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);<br /> // 得到Server EndPoint<br /> try<br /> {<br /> serverHE = Dns.GetHostByName(host);<br /> }<br /> catch(Exception)<br /> {</p> <p> return "没有发现主机";<br /> }</p> <p> // 把 Server IP_EndPoint转换成EndPoint<br /> IPEndPoint ipepServer = new IPEndPoint(serverHE.AddressList[0], 0);<br /> EndPoint epServer = (ipepServer);</p> <p> // 设定客户机的接收Endpoint<br /> fromHE = Dns.GetHostByName(Dns.GetHostName());<br /> IPEndPoint ipEndPointFrom = new IPEndPoint(fromHE.AddressList[0], 0);<br /> EndPoint EndPointFrom = (ipEndPointFrom);</p> <p> int PacketSize = 0;<br /> IcmpPacket packet = new IcmpPacket();</p> <p> // 构建要发送的包<br /> packet.Type = ICMP_ECHO; //8<br /> packet.SubCode = 0;<br /> packet.CheckSum = UInt16.Parse("0");<br /> packet.Identifier = UInt16.Parse("45");<br /> packet.SequenceNumber = UInt16.Parse("0");<br /> int PingData = 32; // sizeof(IcmpPacket) - 8;<br /> packet.Data = new Byte[PingData];</p> <p> // 初始化Packet.Data<br /> for (int i = 0; i 0)<br /> {<br /> dwStop = System.Environment.TickCount - dwStart; // stop timing<br /> return "Reply from "+epServer.ToString()+" in "<br /> +dwStop+"ms. Received: "+nBytes+ " Bytes.";</p> <p> }<br /> timeout=System.Environment.TickCount - dwStart;<br /> if(timeout&gt;1000)<br /> {<br /> return "超时" ;<br /> }<br /> }</p> <p> //close the socket<br /> socket.Close();<br /> return "";<br /> }<br /> ///<br /> <summary> /// This method get the Packet and calculates the total size<br /> /// of the Pack by converting it to byte array<br /> /// </summary> <p>public static Int32 Serialize(IcmpPacket packet, Byte[] Buffer,<br /> Int32 PacketSize, Int32 PingData )<br /> {<br /> Int32 cbReturn = 0;<br /> // serialize the struct into the array<br /> int Index=0;</p> <p> Byte [] b_type = new Byte[1];<br /> b_type[0] = (packet.Type);</p> <p> Byte [] b_code = new Byte[1];<br /> b_code[0] = (packet.SubCode);</p> <p> Byte [] b_cksum = BitConverter.GetBytes(packet.CheckSum);<br /> Byte [] b_id = BitConverter.GetBytes(packet.Identifier);<br /> Byte [] b_seq = BitConverter.GetBytes(packet.SequenceNumber);</p> <p> Array.Copy( b_type, 0, Buffer, Index, b_type.Length );<br /> Index += b_type.Length;</p> <p> Array.Copy( b_code, 0, Buffer, Index, b_code.Length );<br /> Index += b_code.Length;</p> <p> Array.Copy( b_cksum, 0, Buffer, Index, b_cksum.Length );<br /> Index += b_cksum.Length;</p> <p> Array.Copy( b_id, 0, Buffer, Index, b_id.Length );<br /> Index += b_id.Length;</p> <p> Array.Copy( b_seq, 0, Buffer, Index, b_seq.Length );<br /> Index += b_seq.Length;</p> <p> // copy the data<br /> Array.Copy( packet.Data, 0, Buffer, Index, PingData );<br /> Index += PingData;<br /> if( Index != PacketSize/* sizeof(IcmpPacket) */)<br /> {<br /> cbReturn = -1;<br /> return cbReturn;<br /> }</p> <p> cbReturn = Index;<br /> return cbReturn;<br /> }<br /> ///<br /> <summary> /// This Method has the algorithm to make a checksum<br /> /// </summary> <p>public static UInt16 checksum( UInt16[] buffer, int size )<br /> {<br /> Int32 cksum = 0;<br /> int counter;<br /> counter = 0;</p> <p> while ( size &gt; 0 )<br /> {<br /> UInt16 val = buffer[counter];</p> <p> cksum += Convert.ToInt32( buffer[counter] );<br /> counter += 1;<br /> size -= 1;<br /> }</p> <p> cksum = (cksum &gt;&gt; 16) + (cksum &amp; 0xffff);<br /> cksum += (cksum &gt;&gt; 16);<br /> return (UInt16)(~cksum);<br /> }<br /> }<br /> /// 类结束<br /> ///<br /> <summary> /// Class that holds the Pack information<br /> /// </summary> <p>public class IcmpPacket<br /> {<br /> public Byte Type; // type of message<br /> public Byte SubCode; // type of sub code<br /> public UInt16 CheckSum; // ones complement checksum of struct<br /> public UInt16 Identifier; // identifier<br /> public UInt16 SequenceNumber; // sequence number<br /> public Byte [] Data;</p> <p>} // class IcmpPacket<br />

抱歉!评论已关闭.