برنامه ای برای Ping ( پینگ ) گرفتن

دوشنبه 13 خرداد 1392

برنامه ای برای Ping ( پینگ ) گرفتن <br/> در این مقاله کدهای مربوط به Ping گرفتن از اینترنت که در using System.Net.NetworkInformation; قرار دارد را مورد بررسی قرار میدهیم.

در این مقاله کدهای مربوط به Ping گرفتن از اینترنت که در using System.Net.NetworkInformation; قرار دارد را مورد بررسی قرار میدهیم.

برای شروع برنامه به Namespace های زیر نیاز داریم:

using System;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.NetworkInformation;

در این برنامه ما احتیاج به یک button و textbox داریم که آدرس سایت مورد نظر ما را دریافت کند. کد مربوط به button ما به شرح زیر است:

        private void btnPing_Click(object sender, EventArgs e)
        {
            // Reset the number of pings
            pingsSent = 0;
            // Clear the textbox of any previous content
            txtResponse.Clear();
            txtResponse.Text += "Pinging " + txtIP.Text + " with 32 bytes of data:\r\n\r\n";
            // Send the ping
            SendPing();
        }

در اینجا کلید ما متد SendPing() را فراخوانی میکند:

        private void SendPing()
        {
            lblResponse.Text = "Response:";
            txtIP.Enabled = false;
            System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping();
            // Create an event handler for ping complete
            pingSender.PingCompleted += new PingCompletedEventHandler(pingSender_Complete);
            // Create a buffer of 32 bytes of data to be transmitted.
            byte[] packetData = Encoding.ASCII.GetBytes("................................");
            // Jump though 50 routing nodes tops, and don't fragment the packet
            PingOptions packetOptions = new PingOptions(50, true);
            // Send the ping asynchronously
            if (!string.IsNullOrEmpty(txtIP.Text))
                pingSender.SendAsync(txtIP.Text, 5000, packetData, packetOptions, resetEvent);
            else
            {
                MessageBox.Show("Please Enter IP Address!", "Note", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtResponse.Text += "Please Enter IP Address!";
                txtIP.Enabled = true;
            }
        }

حال برای چک کردن سایت از Ping یک PingCompletedEventHandler می سازیم و اطلاعات بدست آمده را چک می کنیم:

        private void pingSender_Complete(object sender, PingCompletedEventArgs e)
        {
            // If the operation was canceled, display a message to the user.
            if (e.Cancelled)
            {
                txtResponse.Text += "Ping was canceled...\r\n";
                // The main thread can resume
                ((AutoResetEvent)e.UserState).Set();
                txtIP.Enabled = true;
            }
            else if (e.Error != null)
            {
                //txtResponse.Text += "An error occured: " + e.Error + "\r\n";
                MessageBox.Show("Enter A Correct IP Address", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtResponse.Text += "Enter A Correct IP Address";
                // The main thread can resume
                ((AutoResetEvent)e.UserState).Set();
                txtIP.Enabled = true;
            }
            else
            {
                PingReply pingResponse = e.Reply;
                // Call the method that displays the ping results, and pass the information with it
                ShowPingResults(pingResponse);
            }
        }

و در آخر برای برگردان خروجی از متد ShowPingResults() استفاده میکنیم:

public void ShowPingResults(PingReply pingResponse)
        {
            if (pingResponse == null)
            {
                // We got no response
                txtResponse.Text += "There was no response.\r\n";
                return;
            }
            else if (pingResponse.Status == IPStatus.Success)
            {
                // We got a response, let's see the statistics
                txtResponse.Text += ++i + " - Reply from " + pingResponse.Address + ": bytes=" + pingResponse.Buffer.Length + " time=" + pingResponse.RoundtripTime + " TTL=" + pingResponse.Options.Ttl + "\r\n";
                ActiveForm.Text = "Round trip time = " + pingResponse.RoundtripTime + "ms";
            }
            else
            {
                // The packet didn't get back as expected, explain why
                txtResponse.Text += ++i + " - Ping was unsuccessful: " + pingResponse.Status + "\r\n";
            }
            // Increase the counter so that we can keep track of the pings sent
            pingsSent++;
            // Send 4 pings
            if (pingsSent < 5)
            {
                SendPing();
            }
            else
            {
                txtResponse.Text += "\r\nhttp://www.madaeny.com\r\n";
                lblResponse.Text += " Complete.";
                txtIP.Enabled = true;
            }
        }


فایل های ضمیمه

سجاد باقرزاده

نویسنده 54 مقاله در برنامه نویسان
  • C#.net
  • 5k بازدید
  • 3 تشکر

کاربرانی که از نویسنده این مقاله تشکر کرده اند

در صورتی که در رابطه با این مقاله سوالی دارید، در تاپیک های انجمن مطرح کنید