微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#Ping我的世界

因此找到了这个小代码段,使您可以在PHP中对minecraft服务器执行ping操作,但是现在我想在C#中执行此操作.

我尝试自己做,但由于某种原因它无法正常工作

        UdpClient client = new UdpClient();
        IPEndPoint ep;
        try
        {
            ep = new IPEndPoint(IPAddress.Parse("-snip-"), -snip-);
            client.Connect(ep);
        }
        catch { Console.WriteLine("Error"); Console.ReadLine(); return; }
        byte[] bytes = new byte[1];
        bytes[0] = (byte)0xFE;
        client.Send(bytes, bytes.Length);
        IPEndPoint rep = new IPEndPoint(IPAddress.Any, 0);
        byte[] recv = client.Receive(ref rep);
        Console.WriteLine(ASCIIEncoding.ASCII.GetString(recv));
        Console.ReadLine();

服务器似乎只是完全忽略了该数据包.这是我发现的代码片段:

    $fp = fsockopen($host, $port, $errno, $errstr, $timeout);
    if (!$fp) return false;

    //Send 0xFE: Server list ping

    fwrite($fp, "\xFE");

    //Read as much data as we can (max packet size: 241 bytes)
    $d = fread($fp, 256);

    //Check we've got a 0xFF disconnect
    if ($d[0] != "\xFF") return false;

有人可以指出我犯了什么错误吗?谢谢!

解决方法:

如描述的here

The client initiates a TCP connection to the minecraft server on the
standard port. Instead of doing auth and logging in (as detailed in
Protocol Encryption), it sends the two byte sequence FE 01. This is a
0xFE server list ping packet. If the second byte (the 0x01) is
missing, the server waits about 1000ms then replies with the Server ->
Client format used in 1.3 and earlier.

您需要发送TCP请求,而发送UDP数据包…

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐