C # ping minecraft

So, I found this small piece of code that will allow you to ping the Minecraft server in PHP, but now I want to do it in C #.

I tried to do it myself, but for some reason it just didn't work

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

Can someone please point out what error I am making? Thanks!

+7
source share
1 answer

As described here

The client initiates a TCP connection to the minecraft server on a standard port. Instead of authorizing and logging in (as specified in Protocol Encryption), it sends two byte sequences FE 01. This is a 0xFE list of ping packets. If the second byte (0x01) is equal to absent, the server waits for about 1000 ms, and then responds to Server → Client format used in 1.3 and earlier versions.

you need to send a TCP request while you are sending a UDP packet ...

+9
source

All Articles