如何检查特定IP地址是否连接到网络

3

我希望创建一个应用程序,以查找网络上特定IP地址是否在线。我已经知道这个IP地址。虽然我对C#并不是很熟悉,但我想知道是否有人能提供一个简单的解决方案。谢谢。


1
你想要获取已知的IP地址吗? - Karolis Kajenas
是的,@Carl,现在只有一个。 - LUKER
如果您已经知道IP地址,那么您需要“查找”什么? - Preston Guillot
我想知道 IP 地址是否在网络上 @PrestonGuillot - LUKER
https://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping(v=vs.110).aspx - Preston Guillot
1个回答

5
Ping ping = new Ping ();
IPAddress address = IPAddress.Parse("000.000.000.000");
PingReply pong = pingSender.Send(address);

pong 对象包含了其是否成功的信息。

if (pong.Status == IPStatus.Success)
{
  // your machine at address is up and responding
}

完整的程序会使用这个。
using System;
using System.Net;
using System.Net.NetworkInformation;

public class Program
{
    public static void Main()
    {
        Ping ping = new Ping();
        //change the following ip variable into the ip adress you are looking for
        string ip = " ";
        IPAddress address = IPAddress.Parse(ip);
        PingReply pong = ping.Send(address);
        if (pong.Status == IPStatus.Success)
        {
            Console.WriteLine(ip + " is up and running.");
        }
    }
}

1
@LUKER 我已经编辑答案并提供了完整的示例。 - pijemcolu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接