C# ping 我的世界

标签 c# php ping minecraft

因此找到了这个小代码片段,它允许您使用 PHP ping Minecraft 服务器,但现在我想在 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.

您需要在发送 UDP 数据包时发送 TCP 请求...

关于C# ping 我的世界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15693583/

相关文章:

c# - 通过鼠标单击获取图表上的 x 坐标

c# - 不是表单时使用 InvokeRequired

C# VSTO Excel 自动筛选值和空白

php - 无法在单个产品页面中重新排序产品标签?

c++ - 将库添加到链接器

java - 无法运行程序 "ping": CreateProcess error=8

javascript - 从代码隐藏调用 js 函数(不是 startupScript)

php - Mysql带条件的子查询,不同的结果

javascript - 将动态内容加载到页面

mysql - 错误 2003 : can't connect on '192.168.1.180' (10061)?