8

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#.

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!

user2073973
  • 564
  • 6
  • 21
  • probably want to remove the IP considering this is a completely public place. – Kyle C Mar 28 '13 at 22:57
  • The first code snippet is what i built in C#, the second one is the snippet in PHP which i found here somewhere on stackoverflow – user2073973 Mar 28 '13 at 22:58

1 Answers1

11

As described 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.

you need to send a TCP request whereas you're sending an UDP packet...

fvu
  • 32,488
  • 6
  • 61
  • 79
  • Oh wow, i figured since minecraft uses the UDP protocol, it would also use the UDP protocol for the ping packet. Thank you! – user2073973 Mar 28 '13 at 22:59
  • @user2073973 Good form is to accept an answer if it solves your problem. – Yaur Mar 28 '13 at 23:08
  • @Yaur Ah yes, i tried voting your answer up but it said i needed 15 rep, i am kind of new to stackoverflow. I marked your answer as the solution now. – user2073973 Mar 28 '13 at 23:15