How can I test a connection to a server with C# given the server's IP address?

前端 未结 4 1374
野的像风
野的像风 2020-12-30 04:15

How can I programmatically determine if I have access to a server with a given IP address using C#?

4条回答
  •  不思量自难忘°
    2020-12-30 04:59

    You could use the Ping class (.NET 2.0 and above)

        Ping x = new Ping();
        PingReply reply = x.Send(IPAddress.Parse("127.0.0.1"));
    
        if(reply.Status == IPStatus.Success)
            Console.WriteLine("Address is accessible");
    

    You might want to use the asynchronous methods in a production system to allow cancelling, etc.

提交回复
热议问题