简体   繁体   English

C#Ping我的世界

[英]C# ping minecraft

So found this little code snippet that would allow you to ping a Minecraft server in PHP, but now i want to do this in C#. 因此找到了这个小代码段,使您可以在PHP中对Minecraft服务器执行ping操作,但是现在我想在C#中执行此操作。

I tried doing this on my own but for some reason its just not working 我尝试自己做,但由于某种原因它无法正常工作

        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();

The server seems to just completely ignore the packet. 服务器似乎只是完全忽略了该数据包。 This is the code snippet i found: 这是我发现的代码片段:

    $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;

Could anyone please point out what mistake i'm making? 有人可以指出我犯了什么错误吗? Thank you! 谢谢!

As described here 如上所述这里

The client initiates a TCP connection to the minecraft server on the standard port. 客户端在标准端口上启动与Minecraft服务器的TCP连接。 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. 它不执行身份验证和登录(如协议加密中所述),而是发送两个字节的序列FE01。这是一个0xFE服务器列表ping数据包。 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. 如果缺少第二个字节(0x01),则服务器将等待约1000ms,然后以1.3及更早版本中使用的Server-> Client格式进行回复。

you need to send a TCP request whereas you're sending an UDP packet... 您需要发送TCP请求,而发送UDP数据包...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM