I tried to communicate with Minecraft Server by using C#. What I try to do is receive information about server (name, motd, icon etc.) by pinging it like a client. I used information from here and here but I can't get this to work.
I can connect and send bytes to it but when I read the response from server I get only a byte "00FF". There's no error's or anything.
What am I doing wrong?
Sample code I'm using:
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);
}
EDIT:
I found out that I was trying to do legacy style ping. It's not working anymore and Ican't find nowhere an updated pinger. I need to send following data but I don't know how to do it:
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.
The full connection info can be found here, but I don't know how to implement this on C#.
This is NOT duplicate because all other questions are for the legacy way.