如何使用UDP ping远程服务器



所以我试图得到的框架设置,使不和谐机器人,将检查,看看如果一个用户的专用服务器是开放的,并相应地更新消息。我目前在本地网络的另一台电脑上运行Minecraft和Valheim服务器。

我试图使用Ping类微软,但它不能Ping Valheim服务器。似乎Valheim正在使用UDP协议,所以我尝试使用UdpClient类来发送消息,但它仍然在waitingforactivation_quot;状态,我不知道发送什么来获得响应。

是否有一个服务器可以识别为ping的标准消息?我已经能够用具有UDP模式的站点ping端口,因此我确定它是打开的并且能够ping通。

这是我尝试ping

的代码
UdpClient udpPing = new UdpClient();
Byte[] sentBytes = Encoding.ASCII.GetBytes("Ping");
udpPing.Connect(servers[i].ip, servers[i].port);
udpPing.Send(sentBytes,sentBytes.Length);
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, servers[i].port);
Task<UdpReceiveResult> result = udpPing.ReceiveAsync();
bool error = false;
for (int y = 0; result.Status == TaskStatus.WaitingForActivation && !error; y++)
{
if(y == tryLimit)
{
error = true;
}
}
udpPing.Close();```

为什么不使用Ping类呢?从那一页:

using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
namespace Examples.System.Net.NetworkInformation.PingTest
{
public class PingExample
{
// args[0] can be an IPaddress or host name.
public static void Main (string[] args)
{
Ping pingSender = new Ping ();
PingOptions options = new PingOptions ();
// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
options.DontFragment = true;
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);
int timeout = 120;
PingReply reply = pingSender.Send (args[0], timeout, buffer, options);
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);
}
}
}
}

最新更新