LoginSignup
5
5

More than 5 years have passed since last update.

IPAddressへPingを送信<C#>

Posted at

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.NetworkInformation;
namespace Sample
{
class Program
{
static void Main(string[] args)
{
Ping p = new Ping();
/送信先IPAddress/
PingReply pr = p.Send("127.0.0.1");
if(pr.Status == IPStatus.Success)
{
Console.WriteLine("ping --- OK");
}
else
{
Console.WriteLine("ping --- NO");
}
}
}
}

上のはコード上で送信先IPAddressを指定しています。
次はソフト上で指定できるようにしてみましょう。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.NetworkInformation;
namespace Sample
{
class Program
{
static void Main(string[] args)
{
Ping p = new Ping();
Console.Write("IPAddress :: ");
var ip = Console.ReadLine();
PingReply pr = p.Send(ip);
if (pr.Status == IPStatus.Success)
{
Console.WriteLine("ping --- OK");
}
else
{
Console.WriteLine("ping --- NO");
}
}
}
}

これでソフト上で指定できるようになりました。

var ip = Console.ReadLine() 👈これでユーザーが入力できるようになっています。
                 そしてvar変数で、
                 変数名ipをpingReply pr = p.Send(ここに変数名)こうしてp.Sendに収めます。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5