使用C#进行Minecraft服务器Ping

时间:2016-01-08 15:45:45

标签: c# server ping tcpclient minecraft

我尝试使用C#与Minecraft Server进行通信。我尝试做的是通过像客户端一样ping它来接收有关服务器(名称,motd,图标等)的信息。我使用了来自herehere的信息,但我无法使其发挥作用。

我可以连接并向其发送字节但是当我从服务器读取响应时,我只得到一个字节" 00FF"。没有任何错误或任何错误。

我做错了什么?

示例代码I使用:

try
        {
            Console.WriteLine("Connecting...");
            TcpClient tcpClient = new TcpClient("127.0.0.1", 25565);
            NetworkStream netStream = tcpClient.GetStream();
            Console.WriteLine("Connected.");

            if (netStream.CanWrite)
            {
                Console.WriteLine("Writing...");
                byte[] bytes = { 0xFE };
                netStream.Write(bytes, 0, bytes.Length);
                Console.WriteLine("Written.");
            }
            else
            {
                Console.WriteLine("Can't write to the stream!");
            }

            if (netStream.CanRead)
            {
                Console.WriteLine("Reading...");
                byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
                netStream.Read(bytes, 0, bytes.Length);

                richTextBox1.AppendText(Encoding.Default.GetString(bytes));

                Console.WriteLine("Read.");
            }
            else
            {
                Console.WriteLine("Can't read from the stream!");
            }

            Console.WriteLine("Closing...");
            netStream.Close();
            tcpClient.Close();
            Console.WriteLine("Closed.");
        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message);
        }

修改

我发现我正在尝试做传统风格的ping。它不再工作了,我无法找到更新的pinger。我需要发送以下数据,但我不知道该怎么做:

1. FE — packet identifier for a server list ping
2. 01 — server list ping's payload (always 1)
3. FA — packet identifier for a plugin message
4. 00 0B — length of following string, in characters, as a short (always 11)
5. 00 4D 00 43 00 7C 00 50 00 69 00 6E 00 67 00 48 00 6F 00 73 00 74 — the string MC|PingHost encoded as a UTF-16BE string
6. XX XX — length of the rest of the data, as a short. Compute as 7 + len(hostname), where len(hostname) is the number of bytes in the UTF-16BE encoded hostname.
7. XX — protocol version, e.g. 4a for the last version (74)
8. XX XX — length of following string, in characters, as a short
9. ... — hostname the client is connecting to, encoded as a UTF-16BE string
10. XX XX XX XX — port the client is connecting to, as an int. 

可以找到完整的连接信息here,但我不知道如何在C#上实现此功能。

这不是重复的,因为所有其他问题都是针对遗留方式的。

0 个答案:

没有答案