ASP.NET Core使用Ping判断网络是否接通

        static void Main(string[] args)
        {
            // 主机地址
            string targetHost = "bing.com";
            string data = "Hello world";

            Ping pingSender = new Ping();
            PingOptions options = new PingOptions
            {
                DontFragment = true
            };

            byte[] buffer = Encoding.ASCII.GetBytes(data);
            int timeout = 1024;

            Console.WriteLine($"Pinging {targetHost}");
            //PingReply reply = pingSender.Send(targetHost,timeout,buffer,options);

            PingReply reply = pingSender.Send(targetHost,timeout);

            if (reply.Status == IPStatus.Success)
            {
                Console.WriteLine($"Address: {reply.Address}");
                Console.WriteLine($"RoundTrip time: {reply.RoundtripTime}");
                Console.WriteLine($"Time to live: {reply.Options.Ttl}");
                Console.WriteLine($"Don‘t fragment: {reply.Options.DontFragment}");
                Console.WriteLine($"Buffer size: {reply.Buffer.Length}");
            }
            else
            {
                Console.WriteLine(reply.Status);
            }

            Console.ReadLine();
        }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐