public static bool GetServerInfo(string serverIP, ref byte protocolversion, ref string version) { try { string host; int port; string[] sip = serverIP.Split(':'); host = sip[0]; if (sip.Length == 1) { port = 25565; } else { try { port = Convert.ToInt32(sip[1]); } catch (FormatException) { port = 25565; } } TcpClient tcp = new TcpClient(host, port); byte[] ping = new byte[2] { 0xfe, 0x01 }; tcp.Client.Send(ping, SocketFlags.None); tcp.Client.Receive(ping, 0, 1, SocketFlags.None); if (ping[0] == 0xff) { MinecraftCom ComTmp = new MinecraftCom(); ComTmp.setClient(tcp); string result = ComTmp.readNextString(); Console.ForegroundColor = ConsoleColor.DarkGray; //Console.WriteLine(result.Replace((char)0x00, ' ')); if (result.Length > 2 && result[0] == '§' && result[1] == '1') { string[] tmp = result.Split((char)0x00); protocolversion = (byte)Int16.Parse(tmp[1]); version = tmp[2]; } else { protocolversion = (byte)39; version = "B1.8.1 - 1.3.2"; } Console.WriteLine("Server version : MC " + version + " (protocol v" + protocolversion + ")."); Console.ForegroundColor = ConsoleColor.Gray; return true; } else { Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine("Unexpected answer from the server (is that a Minecraft server ?)"); Console.ForegroundColor = ConsoleColor.Gray; return false; } } catch { Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine("An error occured while attempting to connect to this IP."); Console.ForegroundColor = ConsoleColor.Gray; return false; } }
public static bool GetServerInfo(string serverIP, ref int protocolversion, ref string version) { try { string host; int port; string[] sip = serverIP.Split(':'); host = sip[0]; if (sip.Length == 1) { port = 25565; } else { try { port = Convert.ToInt32(sip[1]); } catch (FormatException) { port = 25565; } } TcpClient tcp = new TcpClient(host, port); byte[] packet_id = getVarInt(0); byte[] protocol_version = getVarInt(4); byte[] server_adress_val = Encoding.UTF8.GetBytes(host); byte[] server_adress_len = getVarInt(server_adress_val.Length); byte[] server_port = BitConverter.GetBytes((ushort)port); Array.Reverse(server_port); byte[] next_state = getVarInt(1); byte[] packet = concatBytes(packet_id, protocol_version, server_adress_len, server_adress_val, server_port, next_state); byte[] tosend = concatBytes(getVarInt(packet.Length), packet); tcp.Client.Send(tosend, SocketFlags.None); byte[] status_request = getVarInt(0); byte[] request_packet = concatBytes(getVarInt(status_request.Length), status_request); tcp.Client.Send(request_packet, SocketFlags.None); MinecraftCom ComTmp = new MinecraftCom(); ComTmp.setClient(tcp); if (ComTmp.readNextVarInt() > 0) //Read Response length { if (ComTmp.readNextVarInt() == 0x00) //Read Packet ID { string result = ComTmp.readNextString(); //Get the Json data if (result[0] == '{' && result.Contains("protocol\":") && result.Contains("name\":\"")) { string[] tmp_ver = result.Split(new string[] { "protocol\":" }, StringSplitOptions.None); string[] tmp_name = result.Split(new string[] { "name\":\"" }, StringSplitOptions.None); if (tmp_ver.Length >= 2 && tmp_name.Length >= 2) { protocolversion = atoi(tmp_ver[1]); version = tmp_name[1].Split('"')[0]; Console.ForegroundColor = ConsoleColor.DarkGray; //Console.WriteLine(result); //Debug: show the full Json string Console.WriteLine("Server version : " + version + " (protocol v" + protocolversion + ")."); Console.ForegroundColor = ConsoleColor.Gray; return true; } } } } Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine("Unexpected answer from the server (is that a MC 1.7+ server ?)"); Console.ForegroundColor = ConsoleColor.Gray; return false; } catch { Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine("An error occured while attempting to connect to this IP."); Console.ForegroundColor = ConsoleColor.Gray; return false; } }