C#实现ping程序代码

zlzl245437 贡献于2011-06-06

作者 zl  创建于2011-06-06 06:45:00   修改者zl  修改于2011-06-06 06:45:00字数5331

文档摘要:
关键词:

using System.Net; using System.Net.Sockets; using System; namespace Ping { /// /// Class1 的摘要说明。 /// 0) { //显示相应结果 if (elapseTime == 0) { Console.WriteLine("Reply from " + ReceiveBuffer[12] + "." + ReceiveBuffer[13] + "." + ReceiveBuffer[14] + "." + ReceiveBuffer[15] + " to " + ReceiveBuffer[16] + "." + ReceiveBuffer[17] + "." + ReceiveBuffer[18] + "." + ReceiveBuffer[19] + ": icmp_seq=" + i + " time<1ms bytes=" + packet.Data.Length + " TTL=" + ReceiveBuffer[8].ToString()); } else { Console.WriteLine("Reply from " + ReceiveBuffer[12] + "." + ReceiveBuffer[13] + "." + ReceiveBuffer[14] + "." + ReceiveBuffer[15] + " to " + ReceiveBuffer[16] + "." + ReceiveBuffer[17] + "." + ReceiveBuffer[18] + "." + ReceiveBuffer[19] + ": icmp_seq=" + i + " time=" + elapseTime + "ms bytes=" + packet.Data.Length + " TTL=" + ReceiveBuffer[8].ToString()); } } } Console.WriteLine("Ping statistics for " + epServer.ToString()+":"); Console.WriteLine("Packets: Sent = 4," + "Received = " + (4 - icmpFailureNum) + ", Lost = " + icmpFailureNum + " (" + (double)((icmpFailureNum) / 4 * 100) + "% loss)"); //关闭套接字 socket.Close(); } public static UInt16 checksum(UInt16[] buffer, int size) { Int32 cksum = 0; int counter; counter = 0; while (size > 0) { UInt16 val = buffer[counter]; cksum += Convert.ToInt32(buffer[counter]); counter += 1; size -= 1; } cksum = (cksum >> 16) + (cksum & 0xffff); cksum += (cksum >> 16); return (UInt16)(~cksum); } public static Int32 Serialize(IcmpPacket packet, Byte[] Buffer, Int32 PacketSize, Int32 PingData) { Int32 cbReturn = 0; int Index = 0; Byte[] b_type = new Byte[1]; b_type[0] = (packet.Type); Byte[] b_code = new Byte[1]; b_code[0] = (packet.SubCode); Byte[] b_cksum = BitConverter.GetBytes(packet.CheckSum); Byte[] b_id = BitConverter.GetBytes(packet.Identifier); Byte[] b_seq = BitConverter.GetBytes(packet.SequenceNumber); Array.Copy(b_type, 0, Buffer, Index, b_type.Length); Index += b_type.Length; Array.Copy(b_code, 0, Buffer, Index, b_code.Length); Index += b_code.Length; Array.Copy(b_cksum, 0, Buffer, Index, b_cksum.Length); Index += b_cksum.Length; Array.Copy(b_id, 0, Buffer, Index, b_id.Length); Index += b_id.Length; Array.Copy(b_seq, 0, Buffer, Index, b_seq.Length); Index += b_seq.Length; //复制数据 Array.Copy(packet.Data, 0, Buffer, Index, PingData); Index += PingData; if (Index != PacketSize) { cbReturn = -1; return cbReturn; } cbReturn = Index; return cbReturn; } /// /// 应用程序的主入口点。 /// [STAThread] static void Main(string[] argv) { if (argv.Length == 0) { Console.WriteLine("Command format:Ping [/r]"); Console.WriteLine("r/ for loop tests."); } else if (argv.Length == 1) { PingHost(argv[0]); } else if (argv.Length == 2) { //向目的计算机循环发送ICMP数据包 if (argv[1] == "/r") { while (true) { //调用PingHost方法发送ICMP数据包 PingHost(argv[0]); } } else { //向目的计算机发送一次ICMP数据包 PingHost(argv[0]); } } else { //错误参数处理 Console.WriteLine("Error in Arguments"); } } } }

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 15 金币 [ 分享文档获得金币 ] 1 人已下载

下载文档