• Wince C#调用ping


    using System; 
    using System.Drawing; 
    using System.Collections; 
    using System.Windows.Forms; 
    using System.Net; 
    using System.Net.Sockets; 
    using System.Runtime.InteropServices; 
    using System.Text; 
    //Download by http://www.codefans.net 
    namespace Ping 
    { 
        /// <summary> 
        /// Summary description for Form1. 
        /// </summary> 
        public class Form1 : System.Windows.Forms.Form 
        { 
            #region Form Data 
            private System.Windows.Forms.TextBox txtHost; 
            private System.Windows.Forms.Label lblHost; 
            private System.Windows.Forms.ListBox listBox1; 
            private System.Windows.Forms.Button btnPing; 
            private System.Windows.Forms.NumericUpDown udCount; 
            private System.Windows.Forms.Label label1; 
            private System.Windows.Forms.Label label2; 
            private System.Windows.Forms.MainMenu mainMenu1; 
            #endregion 
     
            public Form1() 
            { 
                // 
                // Required for Windows Form Designer support 
                // 
                InitializeComponent(); 
     
                txtHost.Text = "www.yahoo.com"; 
            } 
            /// <summary> 
            /// Clean up any resources being used. 
            /// </summary> 
            protected override void Dispose( bool disposing ) 
            { 
                base.Dispose( disposing ); 
            } 
            #region Windows Form Designer generated code 
            /// <summary> 
            /// Required method for Designer support - do not modify 
            /// the contents of this method with the code editor. 
            /// </summary> 
            private void InitializeComponent() 
            { 
                this.mainMenu1 = new System.Windows.Forms.MainMenu(); 
                this.txtHost = new System.Windows.Forms.TextBox(); 
                this.btnPing = new System.Windows.Forms.Button(); 
                this.lblHost = new System.Windows.Forms.Label(); 
                this.listBox1 = new System.Windows.Forms.ListBox(); 
                this.udCount = new System.Windows.Forms.NumericUpDown(); 
                this.label1 = new System.Windows.Forms.Label(); 
                this.label2 = new System.Windows.Forms.Label(); 
                //  
                // txtHost 
                //  
                this.txtHost.Location = new System.Drawing.Point(40, 16); 
                this.txtHost.Text = "192.168.0.2"; 
                //  
                // btnPing 
                //  
                this.btnPing.Location = new System.Drawing.Point(168, 40); 
                this.btnPing.Size = new System.Drawing.Size(64, 20); 
                this.btnPing.Text = "Ping"; 
                this.btnPing.Click += new System.EventHandler(this.btnPing_Click); 
                //  
                // lblHost 
                //  
                this.lblHost.Location = new System.Drawing.Point(144, 16); 
                this.lblHost.Size = new System.Drawing.Size(96, 20); 
                //  
                // listBox1 
                //  
                this.listBox1.Location = new System.Drawing.Point(0, 72); 
                this.listBox1.Size = new System.Drawing.Size(240, 170); 
                //  
                // udCount 
                //  
                this.udCount.Location = new System.Drawing.Point(48, 40); 
                this.udCount.Minimum = new System.Decimal(new int[] { 
                                                                        1, 
                                                                        0, 
                                                                        0, 
                                                                        0}); 
                this.udCount.Size = new System.Drawing.Size(48, 20); 
                this.udCount.Value = new System.Decimal(new int[] { 
                                                                      4, 
                                                                      0, 
                                                                      0, 
                                                                      0}); 
                //  
                // label1 
                //  
                this.label1.Location = new System.Drawing.Point(0, 16); 
                this.label1.Size = new System.Drawing.Size(32, 20); 
                this.label1.Text = "Addr:"; 
                //  
                // label2 
                //  
                this.label2.Location = new System.Drawing.Point(0, 40); 
                this.label2.Size = new System.Drawing.Size(40, 20); 
                this.label2.Text = "Count:"; 
                //  
                // Form1 
                //  
                this.Controls.Add(this.label2); 
                this.Controls.Add(this.label1); 
                this.Controls.Add(this.udCount); 
                this.Controls.Add(this.listBox1); 
                this.Controls.Add(this.lblHost); 
                this.Controls.Add(this.btnPing); 
                this.Controls.Add(this.txtHost); 
                this.Menu = this.mainMenu1; 
                this.MinimizeBox = false; 
                this.Text = "Form1"; 
     
            } 
            #endregion 
     
            /// <summary> 
            /// The main entry point for the application. 
            /// </summary> 
     
            static void Main()  
            { 
                Application.Run(new Form1()); 
            } 
     
            private void btnPing_Click(object sender, System.EventArgs e) 
            { 
                string addr = txtHost.Text; 
                IPHostEntry entry = null; 
                try 
                { 
                    entry = Dns.GetHostByName(addr); 
                } 
                catch( SocketException ) 
                { 
                } 
                if (entry==null || entry.AddressList.Length == 0) 
                { 
                    MessageBox.Show("Could not resolve " + addr); 
                    txtHost.Focus(); 
                    return; 
                } 
                lblHost.Text = entry.AddressList[0].ToString(); 
     
                byte[] RequestData = Encoding.ASCII.GetBytes( new string('', 64) ); 
     
                //Allocate ICMP_ECHO_REPLY structure 
                ICMP_ECHO_REPLY reply = new ICMP_ECHO_REPLY(255); 
                reply.DataSize = 255; 
                IntPtr pData = LocalAlloc(LMEM_ZEROINIT, reply.DataSize); 
                reply.Data = pData; 
                IntPtr h = IcmpCreateFile(); 
                uint ipaddr = (uint)entry.AddressList[0].Address; 
                listBox1.Items.Clear(); 
                for ( int i = 0; i < (int)udCount.Value; i++ ) 
                { 
                    uint ret = IcmpSendEcho(h, ipaddr, RequestData, (short)RequestData.Length, IntPtr.Zero, reply._Data, reply._Data.Length, 1000); 
                    int dwErr = 0; 
                    if ( ret == 0 ) 
                    { 
                        dwErr = GetLastError(); 
                        if ( dwErr != 11010 ) // If error is other than timeout - display a message 
                            MessageBox.Show("Failed to ping. Error code: " + dwErr.ToString()); 
                    } 
                    if ( dwErr != 11010 ) 
                        listBox1.Items.Add(string.Format("RTT: {0}, Data Size: {1}; TTL: {2}", reply.RoundTripTime, reply.DataSize, reply.Ttl)); 
                    else 
                        listBox1.Items.Add("Request timed out"); 
                    listBox1.SelectedIndex = listBox1.Items.Count - 1; 
                    Application.DoEvents(); 
                    System.Threading.Thread.Sleep(100); 
                } 
                IcmpCloseHandle(h); 
                LocalFree(reply.Data); 
     
            } 
     
            #region Memory Management 
            [DllImport("coredll")] 
            extern public static IntPtr LocalAlloc(int flags, int size); 
            [DllImport("coredll")] 
            extern public static IntPtr LocalFree(IntPtr pMem); 
     
            const int LMEM_ZEROINIT = 0x40; 
     
            #endregion 
     
     
            #region IPHLPAPI P/Invokes 
            [DllImport("iphlpapi")] 
            extern public static IntPtr IcmpCreateFile (); 
     
            [DllImport("iphlpapi")] 
            extern public static bool IcmpCloseHandle(IntPtr h); 
     
            [DllImport("iphlpapi")] 
            extern public static uint IcmpSendEcho( 
                             IntPtr IcmpHandle,  
                             uint DestinationAddress, 
                             byte[] RequestData,  
                             short RequestSize,  
                             IntPtr /*IP_OPTION_INFORMATION*/ RequestOptions,  
                             byte[] ReplyBuffer, 
                             int ReplySize,  
                             int Timeout );  
     
            #endregion 
     
            [DllImport("coredll")] 
            extern static int GetLastError(); 
        } 
     
     
     
        public class ICMP_ECHO_REPLY  
        { 
            public ICMP_ECHO_REPLY(int size) { data = new byte[size]; } 
            byte[] data; 
            public byte[] _Data { get { return data; } } 
            public int Address { get { return BitConverter.ToInt32(data, 0); } } 
            public int Status { get { return BitConverter.ToInt32(data, 4); } } 
            public int RoundTripTime  { get { return BitConverter.ToInt32(data, 8); } }  
            public short DataSize  { get { return BitConverter.ToInt16(data, 0xc); }  set { BitConverter.GetBytes( value ).CopyTo(data, 0xc);} } 
            public IntPtr Data { get { return new IntPtr(BitConverter.ToInt32(data, 0x10)); }  set { BitConverter.GetBytes( value.ToInt32() ).CopyTo(data, 0x10);} }  
            public byte Ttl {get { return data[0x14]; } } 
            public byte Tos {get { return data[0x15]; } } 
            public byte Flags {get { return data[0x16]; } } 
            public byte OptionsSize {get { return data[0x17]; } } 
            public IntPtr OptionsData { get { return new IntPtr(BitConverter.ToInt32(data, 0x18)); }  set { BitConverter.GetBytes( value.ToInt32() ).CopyTo(data, 0x18);} }  
        } 
     
    } 
    View Code
  • 相关阅读:
    复习题之后缀表达式
    专项训练之枚举
    专项训练之二分
    深夜毒物
    笑面的学校日常(14)最近一次更新2017 04 06
    了不起的竞赛生(7)(最近一次更新2017 04 06)
    水题日常——动态规划 洛谷
    Bzoj 1926: [Sdoi2010]粟粟的书架(二分答案+乱搞+主席树)
    Cogs 1708. 斐波那契平方和(矩阵乘法)
    Codevs 1482 路线统计(矩阵乘法)
  • 原文地址:https://www.cnblogs.com/xuexian/p/10929695.html
Copyright © 2020-2023  润新知