关于C#ping查看丢包率的

作者&投稿:招牲 (若有异议请与网页底部的电邮联系)
C#socket异步通信丢包现象求解答,该怎么处理~

可以用ping命令测试一下丢包率,如果ping命令执行期间没有丢包,说明你的代码有问题,如果ping命令都丢包,那就没你的事了

跑通讯协议,或者在1024尾部加2字节CRC校验码,接收后在验证CRC校验码,或者最简单的加1字节校验和!

using System;  
using System.Collections;  
using System.Data;  
using System.Net;  
using System.Net.Sockets;  
  
  
namespace PPing  
{  
   /// <summary>  
   /// Summary description for Class.  
   /// </summary>  
   ///      Ping类  
   class Ping  
   {  
      //声明常量  
      const int SOCKET_ERROR = -1;  
      const int ICMP_ECHO = 8;  
  
      /// <summary>  
      /// The main entry point for the application.  
      /// </summary>  
      [STAThread]  
      // 程序入口  
      static void Main(string[] args)  
      {  
         //  
         // TODO: Add code to start application here  
         //  
         Ping p = new Ping();  
         Console.WriteLine("请输入要 Ping 的IP或者主机名字:");  
         string MyUrl = Console.ReadLine();  
         Console.WriteLine("正在 Ping " + MyUrl + " ……");  
         Console.Write(p.PingHost(MyUrl));  
         Console.WriteLine();  
         Console.ReadLine();  
      }  
  
      public string PingHost(string host)  
      {  
         // 声明 IPHostEntry  
         IPHostEntry serverHE, fromHE;  
         int nBytes = 0;  
         int dwStart = 0, dwStop = 0;  
  
         //初始化ICMP的Socket  
         Socket socket =   new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);  
         socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);  
         // 得到Server EndPoint  
         try  
         {  
            serverHE = Dns.GetHostByName(host);  
         }  
         catch(Exception)  
         {  
            return "没有发现主机";  
         }  
  
         // 把 Server IP_EndPoint转换成EndPoint  
         IPEndPoint ipepServer = new IPEndPoint(serverHE.AddressList[0], 0);  
         EndPoint epServer = (ipepServer);  
  
         // 设定客户机的接收Endpoint  
         fromHE = Dns.GetHostByName(Dns.GetHostName());  
         IPEndPoint ipEndPointFrom = new IPEndPoint(fromHE.AddressList[0], 0);  
         EndPoint EndPointFrom = (ipEndPointFrom);  
  
         int PacketSize = 0;  
         IcmpPacket packet = new IcmpPacket();  
  
         // 构建要发送的包  
         packet.Type = ICMP_ECHO; //8  
         packet.SubCode = 0;  
         packet.CheckSum = UInt16.Parse("0");  
         packet.Identifier   = UInt16.Parse("45");  
         packet.SequenceNumber = UInt16.Parse("0");  
         int PingData = 32; // sizeof(IcmpPacket) - 8;  
         packet.Data = new Byte[PingData];  
  
  
         // 初始化Packet.Data  
         string Tempstr=@"http://xml.sz.luohuedu.net/xml/#";  
         for (int i = 0; i < PingData; i++)  
         {  
            packet.Data[i] =(byte)Tempstr[i];  
         }  
  
         //Variable to hold the total Packet size  
         PacketSize = PingData + 8;  
         Byte [] icmp_pkt_buffer = new Byte[ PacketSize ];  
         Int32 Index = 0;  
         //Call a Method Serialize which counts  
         //The total number of Bytes in the Packet  
         Index = Serialize(  
            packet,  
            icmp_pkt_buffer,  
            PacketSize,  
            PingData );  
         //Error in Packet Size  
         if( Index == -1 )  
         {  
            return "Error Creating Packet";  
         }  
  
         // convert into a UInt16 array  
  
         //Get the Half size of the Packet  
         Double double_length = Convert.ToDouble(Index);  
         Double dtemp = Math.Ceiling( double_length / 2);  
         int cksum_buffer_length = Convert.ToInt32(dtemp);  
         //Create a Byte Array  
         UInt16 [] cksum_buffer = new UInt16[cksum_buffer_length];  
         //Code to initialize the Uint16 array  
         int icmp_header_buffer_index = 0;  
         for( int i = 0; i < cksum_buffer_length; i++ )  
         {  
            cksum_buffer[i] =BitConverter.ToUInt16(icmp_pkt_buffer,icmp_header_buffer_index);  
            icmp_header_buffer_index += 2;  
         }  
         //Call a method which will return a checksum  
         UInt16 u_cksum = checksum(cksum_buffer, cksum_buffer_length);  
         //Save the checksum to the Packet  
         packet.CheckSum = u_cksum;  
  
         // Now that we have the checksum, serialize the packet again  
         Byte [] sendbuf = new Byte[ PacketSize ];  
         //again check the packet size  
         Index = Serialize(  
            packet,  
            sendbuf,  
            PacketSize,  
            PingData );  
         //if there is a error report it  
         if( Index == -1 )  
         {  
            return "Error Creating Packet";  
         }  
  
         dwStart = System.Environment.TickCount; // Start timing  
         //send the Packet over the socket  
         if ((nBytes = socket.SendTo(sendbuf, PacketSize, 0, epServer)) == SOCKET_ERROR)  
         {  
            return "Socket Error: cannot send Packet";  
         }  
         // Initialize the buffers. The receive buffer is the size of the  
         // ICMP header plus the IP header (20 bytes)  
         Byte [] ReceiveBuffer = new Byte[256];  
         nBytes = 0;  
         //Receive the bytes  
         bool recd =false ;  
         int timeout=0 ;  
  
         //loop for checking the time of the server responding  
         while(!recd)  
         {  
            nBytes = socket.ReceiveFrom(ReceiveBuffer, 256, 0, ref EndPointFrom);  
            if (nBytes == SOCKET_ERROR)  
            {  
               return "主机没有响应" ;  
  
            }  
            else if(nBytes>0)  
            {  
               dwStop = System.Environment.TickCount - dwStart; // stop timing  
               return "Reply from "+epServer.ToString()+" in "  
                  +dwStop+"ms. Received: "+nBytes+ " Bytes.";  
            }  
            timeout=System.Environment.TickCount - dwStart;  
            if(timeout>1000)  
            {  
               return "超时" ;  
            }  
         }  
  
         //close the socket  
         socket.Close();  
         return "";  
      }  
  
      /// <summary>  
      /// This method get the Packet and calculates the total size  
      /// of the Pack by converting it to byte array  
      /// </summary>  
      public static Int32 Serialize(IcmpPacket packet, Byte[] Buffer,  
   I      nt32 PacketSize, Int32 PingData )  
      {  
         Int32 cbReturn = 0;  
         // serialize the struct into the array  
         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;  
  
         // copy the data  
         Array.Copy( packet.Data, 0, Buffer, Index, PingData );  
         Index += PingData;  
         if( Index != PacketSize/* sizeof(IcmpPacket) */)  
         {  
            cbReturn = -1;  
            return cbReturn;  
         }  
  
         cbReturn = Index;  
         return cbReturn;  
      }  
  
      /// <summary>  
      ///      This Method has the algorithm to make a checksum  
      /// </summary>  
      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 class IcmpPacket  
   {  
      public Byte Type;    // type of message  
      public Byte SubCode;    // type of sub code  
      public UInt16 CheckSum;   // ones complement checksum of struct  
      public UInt16 Identifier;      // identifier  
      public UInt16 SequenceNumber;     // sequence number  
      public Byte [] Data;  
   } // class IcmpPacket  
  
}



求于成龙的生平
于成龙(1617年9月26日-1684年5月31日) 字北溟,号于山,清代山西永宁州(今山西省吕梁市方山县)人。崇祯十二年(1639年),于成龙参加乡试 顺治十八年(1661年)广西罗城为县令 康熙六年(1667年),于成龙被两广总督金光祖举荐为广西省唯一“卓异”康熙八年(1669年),于成龙被擢升为湖广黄州府同知...

于成龙简介个人资料
于成龙在二十余年的宦海生涯中,三次被举“卓异”,所到之处,皆有政声。尤其是始终清廉自守,多行善政,深得士民爱戴。康熙帝赞誉他为“天下廉吏第一”,是清代循吏的代表人物。历史评价:康熙曾破例亲自撰碑文并题写“高行清粹”匾额赐给于成龙,评价他为“清官第一,天下第一廉吏” 。康熙帝后来...

于成龙个人介绍
于成龙(1638年~1700年),汉军镶红旗人,字振甲,号如山。历任乐亭知县、通州知州、江宁知府、安徽按察使、直隶巡抚、都察院左都御史、汉军都统、兵部尚书、加总督衔直隶巡抚、河道总督等职。康熙三十九年,病卒于淮安河道总督署,享年六十三岁,谥号襄勤。青少年时代的于成龙是在不断的移居中生活的,颠...

清代康熙名臣于成龙是怎么样的一个人?真的能杀赫里这个大将军吗?_百 ...
在电视剧《于成龙》中,曾出现过这样一出剧情,身为两江总督的于成龙,将大将军赫里拘禁,并最终诛杀。电视剧中虽有虚构成分,但在真实历史上,于成龙、赫里,都是在清朝康熙年间活生生存在过的人,前者为文臣,后者为武将,按理说该是各司其职,互不干涉,有些时候,朝廷中还会出现武强文弱之事。再...

于成龙相当于现在什么官
于成龙相当于现代地方局书记官。于成龙在古代中国的官职体系中有丰富的经历,职务包括但不限于县令、知州、同知、知府、两江总督等,相当于现代的兼任政协副主席的地方局书记,行政级别高于副国级,稍低于正国级。于成龙(1638年至1700年),汉军镶红旗人,字振甲,号如山,历任乐亭知县、通州知州、江宁...

清代康熙名臣于成龙是怎么样的一个人?真能杀赫里这个大将军吗?_百度知 ...
在电视剧《于成龙》中,曾出现过这样一出剧情,身为两江总督的于成龙,将大将军赫里拘禁,并最终诛杀。电视剧中虽有虚构成分,但在真实历史上,于成龙、赫里,都是在清朝康熙年间活生生存在过的人,前者为文臣,后者为武将,按理说该是各司其职,互不干涉,有些时候,朝廷中还会出现武强文弱之事。再...

廉吏于成龙文言文翻译
以下是对“廉吏于成龙”的文言文翻译:原文 于成龙,字北溟,山西永宁人。顺治十八年,以副榜贡生授广西罗城知县,年四十五矣。罗城当万山,中多瘴疠。大盗时扰,居民鲜少。成龙到官,招抚流亡,躬自劝课,给以牛种,民率得安居乐业。累迁至直隶通州知州。翻译 于成龙,字北溟,山西永宁人。顺治十八年...

于成龙的人物简介
于成龙(1638-1700)字振甲,号如山,汉军镶黄旗人,(今河北省固安县)后人又称他为“小于成龙”。他也是一位好官,在老于成龙手下做过知州,还曾得到过老于成龙的保举,后因政绩升任江宁知府。康熙皇帝南巡至江宁,要他好好向老于成龙学习。这也可以算是清史上的一段佳话。康熙七年,任直隶乐亭...

历史上于成龙做过的官分别相当于现在的什么官
1、县令 县令相当于现在的县委书记。顺治十八年(1661年),已44岁的于成龙,不顾亲朋的阻拦,抛妻别子,怀着“此行绝不以温饱为志,誓勿昧天理良心”的抱负,接受清廷委任,到遥远的边荒之地广西罗城为县令。2、知州 知州相当于现在的市委书记。康熙六年(1667年),于成龙被两广总督金光祖举荐为广西...

于成龙真实历史是咋样死的
于成龙真实历史是老死的。康熙二十三年(1684年)农历四月十八日,于成龙在两江总督任上终于走到了人生的最后关头,终年六十八岁。于成龙逝世后,南京男女老幼,商贩僧侣皆痛哭流涕,可见中下层人民对他的死是十分悲痛的。康熙帝破例亲为撰写碑文,这是对他廉洁刻苦一生的表彰。于成龙,字北溟,别号子山...

永兴县19448565769: 关于C#ping查看丢包率的 -
凌文解毒: using System; using System.Collections; using System.Data; using System.Net; using System.Net.Sockets; namespace PPing {/// <summary>/// Summary description for Class./// </summary>/// Ping类class Ping{//声明常量const int ...

永兴县19448565769: 如何使用ping和tracert命令检测丢包 -
凌文解毒: 检测与某个IP之间的连接是否通畅 用ping IP或者域名的方法检测. 如ping www.baidu.com-t或 ping 192.168.1.1 -t.-t参数建议添加,否则只ping 4次. tracert命令是检查主机与目标IP之间所有跳数的连接速度,一般用于详细检测链路不通的故障出现在哪一环时使用. 格式 tracert www.baidu.com

永兴县19448565769: C#socket异步通信丢包现象求解答,该怎么处理 -
凌文解毒: 可以用ping命令测试一下丢包率,如果ping命令执行期间没有丢包,说明你的代码有问题,如果ping命令都丢包,那就没你的事了

永兴县19448565769: 常用ping命令测试网络丢包的原因和解决方法
凌文解毒: 所谓的网络数据包丢失是一种现象,当我们使用ping查询目标站时,由于各种原因,数据包在通道中丢失. Ping使用ICMP回显请求和回显应答消息. ICMP回显请求消息...

永兴县19448565769: C#中如何计算UDP的丢包率? -
凌文解毒: string sResult=string.Empty; bool bResult=false; byte[] buf=new byte[2048]; try { IPEndPoint sender=new IPEndPoint(IPAddress.Any, 2425); EndPoint tempRemoteEP=(EndPoint)sender; IAsyncResult ias=socket.BeginReceiveFrom(buf, 0, 2048, ...

永兴县19448565769: 怎么查丢包率 -
凌文解毒: 运行--在输入栏里输入“cmd”,回车--弹出命令提示符--输入“ping www.baiddu.com(或者别的网址也可以) -t”,回车.就OK了 如下:Microsoft Windows XP [版本 5.1.2600](C) 版权所有 1985-2001 Micro...

永兴县19448565769: C# socket通信 怎么验证丢包率和误码率 -
凌文解毒: 跑通讯协议,或者在1024尾部加2字节CRC校验码,接收后在验证CRC校验码,或者最简单的加1字节校验和!

永兴县19448565769: 如何查看丢包率 -
凌文解毒: 点开始 运行 输入ping 61.139.2.69 -t -t可以不输入,不过PING四次就退出了,输入-t他就一直PING到你手动关闭. 61.139.2.69是四川电信的DNS服务器IP.如果你不是四川的,就输入你们本省的DNS服务器IP.这样才能更准确. 输入后会显示一条条例如:reply from 61.139.2.69 bytes=32 time=20 ttl=249 bytes是发送的数据包大小,time是到达目标时间,这个时间越小越好,ttl表示生存时间. 如果不定时显示Request timed out,则说明你的网络存在掉包现象.

永兴县19448565769: 在运行里输入ping什么可以查看网络是否丢包数值25左右那种哦
凌文解毒: 在运行里输入的 是CMD 然后出来一个窗口,输入ping www.163.comping 后面一个空格,网址可以随便输

永兴县19448565769: 如何检查网络丢包率(要使用命令) -
凌文解毒: 在运行符里输入ping www.baidu.com -n 10 ,ping通后看time的延迟时间. C:\Documents and Settings\Administrator>ping www.baidu.com Pinging www.a.shifen.com [220.181.111.148] with 32 bytes of data: Reply from 220.181.111.148: bytes=32 ...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网