110,534
社区成员
发帖
与我相关
我的任务
分享
static void Main(string[] args)
{
Ping icmpClient = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
byte[] msg = Encoding.UTF8.GetBytes("howdy pilgrim");
while (true)
{
PingReply reply = icmpClient.Send("115.115.115.1", 60 * 1000, msg, options);
string responseReceived = Encoding.UTF8.GetString(reply.Buffer);
Console.WriteLine(DateTime.Now.ToString() + " Response from server: " + responseReceived);
Thread.Sleep(500);
}
}
static void Main(string[] args)
{
while (true)
{
Socket icmpListener = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
icmpListener.Bind(new IPEndPoint(IPAddress.Parse("192.168.1.3"), 0));
icmpListener.IOControl(IOControlCode.ReceiveAll, new byte[] { 1, 0, 0, 0 }, null);
byte[] buffer = new byte[1024*1024];
EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
int bytesRead = icmpListener.ReceiveFrom(buffer, ref remoteEndPoint);
string receivedMsg = Encoding.UTF8.GetString(buffer, 28, bytesRead);
Console.WriteLine(DateTime.Now.ToString() + ": Received " + bytesRead + "B from " + remoteEndPoint + ": " + receivedMsg);
byte[] customResponse = Encoding.UTF8.GetBytes("good day");
int bytesSent = icmpListener.SendTo(customResponse, remoteEndPoint);
}
}