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!
user2073973
source share