Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 315
user2073973 Avatar asked Mar 28 '13 22:03

user2073973


1 Answers

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

like image 181
fvu Avatar answered Sep 22 '22 21:09

fvu