【发布时间】:2021-11-04 11:47:53
【问题描述】:
所以我正在尝试设置框架以创建一个不和谐机器人,该机器人将检查用户专用服务器是否打开并相应地更新消息。我目前在本地网络中的另一台计算机上运行 Minecraft 和 Valheim 服务器。
我尝试使用 Microsoft 提供的 Ping 类,但它无法 ping Valheim 服务器。 Valheim 似乎正在使用 UDP 协议,所以我尝试使用 UdpClient 类来发送消息,但它仍然处于“WaitingForActivation”状态,我不知道要发送什么来获得响应。
是否有服务器识别为 ping 的标准消息?我已经能够 ping 具有 UDP 模式的站点的端口,所以我确定它是打开的并且能够被 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();```
【问题讨论】:
标签: c# sockets .net-core discord udp