以编程方式ping到远程服务器 [英] Programmatically ping to remote server

查看:66
本文介绍了以编程方式ping到远程服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我想通过我的c#.net应用程序ping远程服务器.并希望在没有回复的情况下弹出消息.

我在外壳命令上进行了一些R&D尝试.但是没有成功.我想我需要制作BAT文件来捕获shell命令的输出.

现在,我正在考虑使用TCP-IP编程进行相同的操作.我的意思是说,我想以编程方式"对远程服务器执行ping操作.

Hello,

I want to ping a remote server through my c# .net application. And want to pop up the message in case of no reply.

I tried it with some R & D on shell commands. But was unsuccessful. I think I need to make BAT file to catch the output of the shell command.

Now I am thinking of doing the same with using TCP-IP programming. I mean to say, I want to make a ping to the remote server "pro grammatically"..

Any help please??

推荐答案

using System.Net;
using System.Net.NetworkInformation;
using System.ComponentModel;
using System.Threading;
using System.IO;
namespace PingMyServer
{
 public static void Main(string[] args)
        {
            //if (args.Length == 0)
                //throw new ArgumentException("Ping needs a host or IP Address.");

            string who = "someserver.anydomain.net";// args[0]; //fqdn or ip of your server"
            AutoResetEvent waiter = new AutoResetEvent(false);

            Ping pingSender = new Ping();

            // When the PingCompleted event is raised,
            // the PingCompletedCallback method is called.
            pingSender.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback);

            // Create a buffer of 32 bytes of data to be transmitted.
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes(data);

            // Wait 12 seconds for a reply.
            int timeout = 12000;

            // Set options for transmission:
            // The data can go through 64 gateways or routers
            // before it is destroyed, and the data packet
            // cannot be fragmented.
            PingOptions options = new PingOptions(64, true);

            Console.WriteLine("Time to live: {0}", options.Ttl);
            Console.WriteLine("Don't fragment: {0}", options.DontFragment);

            // Send the ping asynchronously.
            // Use the waiter as the user token.
            // When the callback completes, it can wake up this thread.
            pingSender.SendAsync(who, timeout, buffer, options, waiter);

            // Prevent this example application from ending.
            // A real application should do something useful
            // when possible.
            waiter.WaitOne();
            Console.WriteLine("Ping example completed.");
            Console.WriteLine();
        }


        private static void PingCompletedCallback(object sender, PingCompletedEventArgs e)
        {
            // If the operation was canceled, display a message to the user.
            if (e.Cancelled)
            {
                Console.WriteLine("Ping canceled.");

                // Let the main thread resume. 
                // UserToken is the AutoResetEvent object that the main thread 
                // is waiting for.
                ((AutoResetEvent)e.UserState).Set();
            }

            // If an error occurred, display the exception to the user.
            if (e.Error != null)
            {
                Console.WriteLine("Ping failed:");
                Console.WriteLine(e.Error.ToString());

                // Let the main thread resume. 
                ((AutoResetEvent)e.UserState).Set();
            }

            PingReply reply = e.Reply;

            DisplayReply(reply);

            // Let the main thread resume.
            ((AutoResetEvent)e.UserState).Set();
        }

        public static void DisplayReply(PingReply reply)
        {
            if (reply == null)
                return;

            Console.WriteLine("ping status: {0}", reply.Status);
            if (reply.Status == IPStatus.Success)
            {
                Console.WriteLine("Address: {0}", reply.Address.ToString());
                Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
                Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
                Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
                Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
            }
        }
}


使用Ping和PingReply类
http://www.shefeekj.com/ping-ip-address-using-cnet.html [^ ]
Use Ping and PingReply class
http://www.shefeekj.com/ping-ip-address-using-cnet.html[^]


这篇关于以编程方式ping到远程服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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