Jump to content

Server Pinger with C#


LizNet

Recommended Posts

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 http://stackoverflow.com/questions/15693583/c-sharp-ping-minecraft and http://wiki.vg/Server_List_Ping 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:

 

Turned out that I was trying to ping the server by using the legacy way and that's not working anymore.

 

Found a proper connection info, but I don't know how to do it in C#:

 

Client to Server:

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. 


Server to Client:

The server responds with a 0xFF kick packet. The packet begins with a single byte identifier ff, then a two-byte big endian short giving the length of the following string in characters. You can actually ignore the length because the server closes the connection after the response is sent.

After the first 3 bytes, the packet is a UTF-16BE string. It begins with two characters: §1, followed by a null character. On the wire these look like 00 a7 00 31 00 00.

The remainder is null character (that is 00 00) delimited fields:

1. Protocol version (e.g. 74)
2. Minecraft server version (e.g. 1.8.7)
3. Message of the day (e.g. A Minecraft Server)
4. Current player count
5. Max players 

 

Source: http://wiki.vg/Server_List_Ping#1.6

 

If anyone knows how to send right information to the server with C# I'll appreciate it. A lot.  ;D

 

This is also my first post here, so if there's a better section for this please move this topic.  :)

Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.