如何在C#中ping IP [英] How Can Ping IP in C#

查看:63
本文介绍了如何在C#中ping IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。如何在C#中ping IP?

解决方案

你怎么能对此产生疑问? :-)参见 System.Net.NetworkInformation.Ping



-SA


 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Text;
使用 System.Windows.Forms;
使用 System.Threading;
使用 System.Net.NetworkInformation;
命名空间 Ping
{
public partial class Form1:Form
{
// 计算ping
private int pingsSent;
// 可用于通知操作何时完成
AutoResetEvent resetEvent = new AutoResetEvent( false );
public Form1()
{
InitializeComponent();
}
私有 void btnPing_Click( object sender,EventArgs e)
{
// 重置数量ping
pingsSent = 0 ;
// 清除以前任何内容的文本框
txtResponse.Clear() ;
txtResponse.Text + = Ping + txtIP.Text + 32字节数据:\\\\\\\ n;
// 发送ping
SendPing();
}
私有 void SendPing()
{
System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping();
// 为ping完成创建事件处理程序
pingSender.PingCompleted + = new PingCompletedEventHandler(pingSender_Complete);
// 创建一个32字节数据的缓冲区。
byte [] packetData = Encoding.ASCII.GetBytes( ................................);
// 跳过50个路由节点顶部,不要分组数据包
PingOptions packetOptions = new PingOptions( 50 true < /跨度>);
// 异步发送
pingSender.SendAsync(txtIP.Text, 5000 ,packetData,packetOptions,resetEvent);
}
私有 void pingSender_Complete( object sender,PingCompletedEventArgs e)
{
// 如果操作是取消后,向用户显示一条消息。
if (e.Cancelled)
{
txtResponse.Text + = Ping已取消... \\\\ n;
// 主线程可以恢复
((AutoResetEvent)e.UserState )。组();
}
else if (e.Error!= null
{
txtResponse.Text + = 发生错误: + e.Error + \\\\ n ;
// 主线程可以恢复
((AutoResetEvent)e.UserState )。组();
}
else
{
PingReply pingResponse = e.Reply;
// 调用显示ping结果的方法,并用它传递信息
ShowPingResults(pingResponse);
}
}
public void ShowPingResults(PingReply pingResponse)
{
if (pingResponse == null
{
// 我们没有回复
txtResponse.Text + = 没有回复。\\\\\\\ n;
return ;
}
else if (pingResponse.Status == IPStatus.Success)
{
// 我们得到了回复,让我们看看统计数据
txtResponse.Text + = 来自 + pingResponse.Address.ToString()+ :bytes = + pingResponse.Buffer.Length + time = + pingResponse.RoundtripTime + TTL = + pingResponse.Options.Ttl + \\\\ n;
}
其他
{
// < span class =code-comment>数据包未按预期返回,解释原因

txtResponse.Text + = < span class =code-string> Ping失败:
+ pingResponse.Status + \ r\\\
\r\\\
;
}
// 增加计数器,以便我们可以跟踪发送的ping
pingsSent ++;
// 发送4个ping
if (pingsSent& lt; 4
{
SendPing();
}
}
}
}


谢谢兄弟,经过长时间的研究,我得到了代码。

hi peple. How Can Ping IP in C#

解决方案

How could you have a doubt about it? :-) See System.Net.NetworkInformation.Ping.

—SA


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.NetworkInformation;
namespace Ping
{
    public partial class Form1 : Form
    {
        // Counts the pings
        private int pingsSent;
        // Can be used to notify when the operation completes
        AutoResetEvent resetEvent = new AutoResetEvent(false);
        public Form1()
        {
            InitializeComponent();
        }
        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();
        }
        private void SendPing()
        {
            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
            pingSender.SendAsync(txtIP.Text, 5000, packetData, packetOptions, resetEvent);
        }
        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();
            }
            else if (e.Error != null)
            {
                txtResponse.Text += "An error occured: " + e.Error + "\r\n";
                // The main thread can resume
                ((AutoResetEvent)e.UserState).Set();
            }
            else
            {
                PingReply pingResponse = e.Reply;
                // Call the method that displays the ping results, and pass the information with it
                ShowPingResults(pingResponse);
            }
        }
        public void ShowPingResults(PingReply pingResponse)
        {
            if (pingResponse == null)
            {
                // We got no response
                txtResponse.Text += "There was no response.\r\n\r\n";
                return;
            }
            else if (pingResponse.Status == IPStatus.Success)
            {
                // We got a response, let's see the statistics
                txtResponse.Text += "Reply from " + pingResponse.Address.ToString() + ": bytes=" + pingResponse.Buffer.Length + " time=" + pingResponse.RoundtripTime + " TTL=" + pingResponse.Options.Ttl + "\r\n";
            }
            else
            {
                // The packet didn't get back as expected, explain why
                txtResponse.Text += "Ping was unsuccessful: " + pingResponse.Status + "\r\n\r\n";
            }
            // Increase the counter so that we can keep track of the pings sent
            pingsSent++;
            // Send 4 pings
            if (pingsSent &lt; 4)
            {
                SendPing();
            }
        }
    }
}


Thanks bro , after long research I got code where.


这篇关于如何在C#中ping IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆