zoukankan      html  css  js  c++  java
  • C#实现Ping服务器

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace PingDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("请输入IP:");
                var ip = Console.ReadLine();
                var resutl = Ping(ip);
                Console.WriteLine(resutl);
                Console.ReadLine();
            }
    
            /// <summary>  
            /// 是否能 Ping 通指定的主机  
            /// </summary>  
            /// <param name="ip">ip 地址或主机名或域名</param>  
            /// <returns>true 通,false 不通</returns>  
            static bool Ping(string ip)
            {
                try
                {
                    System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
                    System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
                    options.DontFragment = true;
                    string data = "Test Data!";
                    byte[] buffer = Encoding.ASCII.GetBytes(data);
                    int timeout = 5000; // Timeout 时间,单位:毫秒  
                    System.Net.NetworkInformation.PingReply reply = p.Send(ip, timeout, buffer, options);
                    if (reply == null || reply.Status == System.Net.NetworkInformation.IPStatus.Success)
                        return true;
    
                    return false;
                }
                catch (System.Net.NetworkInformation.PingException e)
                {
                    throw new Exception("找不到服务器");
                }
            }
        }
    }
  • 相关阅读:
    Struts2 Hello World
    Struts2入门(1)
    Struts2_day01
    Java Web Model2实战
    Oracle_day04
    SAP调用外部webservice接口
    通用清账程序
    服务器IDOC文件解析程序
    IDOC接口创建步骤
    SAP 本地发送IDOC
  • 原文地址:https://www.cnblogs.com/zhuyongblogs/p/6025228.html
Copyright © 2011-2022 走看看