zoukankan      html  css  js  c++  java
  • c# 判断一个ip通不通 能不能ping通

    方法一: 已经证实能用的.

    • using System;  
    • using System.Collections.Generic;  
    • using System.ComponentModel;  
    • using System.Data;  
    • using System.Drawing;  
    • using System.Text;  
    • using System.Windows.Forms;  
    • using System.Net;  
    • using System.Net.NetworkInformation;  
    •   
    • namespace PingIpAddress  
    • {  
    •     public partial class Form1 : Form  
    •     {  
    •         public Form1()  
    •         {  
    •             InitializeComponent();  
    •         }  
    •          
    •         private void Form1_Load(object sender, EventArgs e)  
    •         {  
    •               
    •               
    •         }   
    •         private Ping pingSender = new Ping();   
    •         private string strIP = "";   
    •         private void button1_Click(object sender, EventArgs e)  
    •         {  
    •             strIP = txtIP.Text;          //要ping的IP地址     第一种方法本人亲自试验过。可以使用  
    •             PingOptions pingOption = new PingOptions();  
    •             pingOption.DontFragment = true;  
    •   
    •             string data = "sendData:goodgoodgoodgoodgoodgood";  
    •             byte[] buffer = Encoding.ASCII.GetBytes(data);  
    •             int timeout = 120;  
    •             PingReply reply = pingSender.Send(strIP, timeout, buffer);  
    •             if (reply.Status == IPStatus.Success)  
    •             {  
    •                 MessageBox.Show("能ping通 ");  
    •             }  
    •             else  
    •             {  
    •                 MessageBox.Show("ping不通");  
    •             }  
    •         }  
    •     }  
    • }  

    第二种方法: 没试过

    • C#判断网络状态  
    • 代码   
    • /// <summary>  
    • /// 是否能 Ping 通指定的主机  
    • /// </summary>  
    • /// <param name="ip">ip 地址或主机名或域名</param>  
    • /// <returns>true 通,false 不通</returns>  
    • public bool Ping(string ip)  
    • {  
    •     int timeout = 1000;  
    •     string data = "Test Data!";  
    •     System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();  
    •     System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();  
    •     options.DontFragment = true;  byte[] buffer = Encoding.ASCII.GetBytes(data);  
    •     System.Net.NetworkInformation.PingReply reply = p.Send(ip, timeout, buffer, options);  
    •     if (reply.Status == System.Net.NetworkInformation.IPStatus.Success) return trueelse return false;  
    • }  
  • 相关阅读:
    Maximum Flow Exhaustion of Paths Algorithm
    ubuntu下安装java环境
    visualbox使用(二)
    vxworks一个超级奇怪的错误(parse error before `char')
    February 4th, 2018 Week 6th Sunday
    February 3rd, 2018 Week 5th Saturday
    February 2nd, 2018 Week 5th Friday
    February 1st, 2018 Week 5th Thursday
    January 31st, 2018 Week 05th Wednesday
    January 30th, 2018 Week 05th Tuesday
  • 原文地址:https://www.cnblogs.com/yuhanzhong/p/3205101.html
Copyright © 2011-2022 走看看