惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
C
Check Point Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
博客园 - 司徒正美
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
V
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
O
OpenAI News
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
I
InfoQ
D
Docker
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - Franky
B
Blog
Scott Helme
Scott Helme
博客园 - 叶小钗
D
DataBreaches.Net

博客园 - myer

使用you-get下载youtube视频 找不到org.springframework.context.ConfigurableApplicationContext的类文件 Centos7安装mysql8 无法加载oramts.dll 反射应用中的同种类型转换错误 - myer - 博客园 aspx文件生产静态html文件 在ASP.NET中实现Url Rewriting 正则表达式 c# 手机号码正则表达式 - myer - 博客园 jscalendar-1.0中文解决方法 GridView中嵌套GridView asp.net 小偷程序实现的原理和源码 - myer - 博客园 GridView 排序 开源软件FtpClient几个问题 CMM简介 DIV模式窗口的实现 .net中序列化和反序列化 ASP.net 本地化和国际化 C#编码规范
C# ping命令的实现
myer · 2007-03-25 · via 博客园 - myer

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 <hostname>.") ;
                Console.WriteLine("       2. MyPing <hostname> <client>.");
                string[] hao = new string[2];
                hao[1] = "localhost";
                hao[0] = "focweb_hao";
                PingHost(hao);
            }    
        }   
        public static void PingHost(string[] hostclient)
        {   
            //    1。解析地址的地址模式,较常用的为AddressFamily.InterNetwork,即IPv4地址。
            //    2。Socket套接字类型,一般为SocketType.Raw原始类型。
            //    3。网络协议类型,这里Ping用的是Internet控制报文协议ProtocolType.Icmp.  
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
          
            IPHostEntry hostInfo;
            try
            {
                hostInfo = Dns.GetHostByName(hostclient[0]);  
            }
            catch(Exception)
            {
                Console.WriteLine("Unknown host {0}.",hostclient[0]);
                return ;
            }
            // 取主机0号端口
            EndPoint hostPoint= (EndPoint)new IPEndPoint(hostInfo.AddressList[0], 0);
            IPHostEntry clientInfo;
            try
            {
                if (hostclient.Length==2)
                    clientInfo =Dns.GetHostByName(hostclient[1]);
                else
                    clientInfo = Dns.GetHostByName(Dns.GetHostName());
            }
            catch(Exception)
            {
                Console.WriteLine("Unknown client {1}.",hostclient[1]);
                return ;
            }
            // 取客户机0号端口       
            EndPoint clientPoint = (EndPoint)new IPEndPoint(clientInfo.AddressList[0], 0);      

            int DataSize = 32; // ICMP数据包大小;
            int PacketSize = DataSize + 8;//总报文长度
            const int ICMP_ECHO = 8;
            IcmpPacket packet = new IcmpPacket(ICMP_ECHO,0,0,45,0,DataSize);
                       
            Byte [] Buffer = new Byte[ PacketSize ];
            int index=packet.ConvertToByte(Buffer);
            if( index != PacketSize)
            {
              Console.WriteLine("There is something wrong with Packet Data !");
              return ;
            }

            int cksum_buffer_length =(int)Math.Ceiling( ((Double)index)/ 2);

            UInt16 [] cksum_buffer = new UInt16[cksum_buffer_length];

            int icmp_header_buffer_index = 0;
            for( int i = 0; i < cksum_buffer_length; i++ )
            {
                  cksum_buffer[i] = BitConverter.ToUInt16(Buffer,icmp_header_buffer_index);
                  icmp_header_buffer_index += 2;
            }


            packet.CheckSum =IcmpPacket.SumOfCheck(cksum_buffer);
            // 保存校验和后,再次将报文转化为数据包
            Byte [] SendData = new Byte[ PacketSize ];
            index= packet.ConvertToByte(SendData);
            if( index != PacketSize)
            {
              Console.WriteLine("There is something wrong with Packet Data !");
              return ;
            }
                          

            int nBytes=0;
            int startTime = Environment.TickCount;
            if ((nBytes = socket.SendTo(SendData, PacketSize, SocketFlags.None,(EndPoint) hostPoint)) == -1)
            {      
              Console.WriteLine("Socket can not send packet");
            }


            Byte [] ReceiveData = new Byte[256];   
            nBytes = 0;

            int timeout=0 ;
            int timeConsume=0;

            while(true)
            {
                nBytes = socket.ReceiveFrom(ReceiveData, 256,SocketFlags.None,ref clientPoint);
                if (nBytes == -1)
                {
                    Console.WriteLine("Host not Responding") ;
                    break;
                }
                else if(nBytes>0)
                {
                    timeConsume = System.Environment.TickCount - startTime;
                    Console.WriteLine("Reply from "+hostPoint.ToString()+" in "
                    +timeConsume+"MS :Bytes Received"+nBytes);
                    break;
                }
                timeConsume=Environment.TickCount - startTime;
                if(timeout>1000)
                {
                    Console.WriteLine("Time Out") ;
                    break;
                }
            }                  
            socket.Close();   
        }
}

    public class IcmpPacket
    {
        private Byte  _type;            // 报文类型
        private Byte  _subCode;        // 字代码类型
        private UInt16 _checkSum;       // 报文校验和
        private UInt16 _identifier;      // 识别符
        private UInt16 _sequenceNumber;     // 序列号
        private Byte [] _data;       

        ///***********************************************************
        ///初始化报文
        ///***********************************************************
        public IcmpPacket(Byte type,Byte subCode,UInt16 checkSum,UInt16 identifier,UInt16 sequenceNumber,int dataSize)
        {
            _type=type;
            _subCode=subCode;
            _checkSum=checkSum;
            _identifier=identifier;
            _sequenceNumber=sequenceNumber;
            _data=new Byte[dataSize];
            for (int i = 0; i < dataSize; i++)
            {
                  _data[i] = (byte)'#';
            }
        }
        public UInt16 CheckSum
        {
            get{ return _checkSum;}
            set{ _checkSum=value; }
        }
      
        ///************************************************************
        ///将整个ICMP报文信息和数据转化为Byte数据包  
        ///************************************************************
        public int ConvertToByte(Byte[] buffer)
        {
            Byte [] b_type = new Byte[1]{_type};
            Byte [] b_code = new Byte[1]{_subCode};
            Byte [] b_cksum = BitConverter.GetBytes(_checkSum);
            Byte [] b_id = BitConverter.GetBytes(_identifier);
            Byte [] b_seq = BitConverter.GetBytes(_sequenceNumber);          
            int i=0;
            Array.Copy( b_type, 0, buffer, i, b_type.Length );
            i+= b_type.Length;
          
            Array.Copy( b_code, 0, buffer, i, b_code.Length );
            i+= b_code.Length;

            Array.Copy( b_cksum, 0, buffer,i, b_cksum.Length );
            i += b_cksum.Length;

            Array.Copy( b_id, 0, buffer, i, b_id.Length );
            i+= b_id.Length;

            Array.Copy( b_seq, 0, buffer, i, b_seq.Length );
            i += b_seq.Length;

            Array.Copy(_data, 0, buffer, i, _data.Length );
            i += _data.Length;

            return i;
        }
        public static UInt16 SumOfCheck( UInt16[] buffer )
        {
            int cksum = 0;
            for(int i=0;i<buffer.Length;i++)
            cksum += (int) buffer[i];

            cksum = (cksum >> 16) + (cksum & 0xffff);
            cksum += (cksum >> 16);
            return (UInt16)(~cksum);    
        }
    }
}