public void Connect(string host, int port = 25565, double timeout = 2.5) { try { Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); if (sock == null) return; sock.Connect(host, port); sock.ReceiveTimeout = (int)(timeout * 1000); sock.SendTimeout = (int)(timeout * 1000); int start = System.Environment.TickCount; sock.Send(new byte[2] { 0xFE, 0x01 }); byte[] buffer = new byte[512]; int recv = sock.Receive(buffer, 0, 512, SocketFlags.None); if (recv < 4 || buffer[0] != 0xFF) return; byte[] nbuf = new byte[recv]; System.Buffer.BlockCopy(buffer, 0, nbuf, 0, recv); string packet = System.Text.Encoding.UTF8.GetString(nbuf).Substring(3); string[] bits; if (packet[1] != 0xA7 && packet[2] != 0x31) { // MC 1.4 and later? bits = packet.Split(new string[1] { "\x00\x00\x00" }, System.StringSplitOptions.None); info = new ServerInfo(); info.Latency = System.Environment.TickCount - start; if (!double.TryParse(bits[1].Replace("\x00", ""), out info.Protocol)) return; info.Version = bits[2].Replace("\x00", ""); info.Name = bits[3].Replace("\x00", ""); if (!int.TryParse(bits[4].Replace("\x00", ""), out info.OnlinePlayers)) return; if (!int.TryParse(bits[5].Replace("\x00", ""), out info.MaxPlayers)) return; success = true; return; } else { // Earlier versions bits = packet.Split(new char[1] { '\xA7' }); info = new ServerInfo(); info.Latency = System.Environment.TickCount - start; info.Version = "1.3"; info.Name = bits[0].Replace("\x00", ""); if (bits.Length > 1) if (!int.TryParse(bits[4].Replace("\x00", ""), out info.OnlinePlayers)) return; if (bits.Length > 2) if (!int.TryParse(bits[5].Replace("\x00", ""), out info.MaxPlayers)) return; success = true; return; } } catch { success = false; } }
internal void GetInfo() { Packet data = this.GrabData(Packets.QueryData(this.SID, this.challenge)); if (data == null) { this.GetChallenge(); return; } if (data.Read<byte>() == (byte)0x00 && data.Read<int>() == this.SID) { info = new ServerInfo(); info.Latency = Environment.TickCount - ping; data.Skip(11); // Unknown padding. string key, value; while (true) { key = data.Read<string>(); value = data.Read<string>(); if (key.Length == 0) break; if (key == "hostname") info.Name = value; else if (key == "gametype") info.GameType = value; else if (key == "game_id") info.GameID = value; else if (key == "version") info.Version = value; else if (key == "plugins") info.Plugins = value; else if (key == "map") info.Map = value; else if (key == "numplayers") { if (!int.TryParse(value, out info.OnlinePlayers)) return; } else if (key == "maxplayers") { if (!int.TryParse(value, out info.MaxPlayers)) return; } else if (key == "hostport") info.HostPort = value; else if (key == "hostip") info.HostIP = value; } data.Skip(1); info.Players = new List<string>(); while (true) { key = data.Read<string>(); if (key.Length == 0) break; info.Players.Add(key); } success = true; } else GetChallenge(); }
internal void GetInfo() { Packet data = this.GrabData(Packets.QueryData(this._sid, this._challenge)); if (data == null) { this.GetChallenge(); return; } if (data.Read <byte>() == (byte)0x00 && data.Read <int>() == this._sid) { _info = new ServerInfo(); _info.Latency = Environment.TickCount - _ping; data.Skip(11); // Unknown padding. string key, value; while (true) { key = data.Read <string>(); value = data.Read <string>(); if (key.Length == 0) { break; } if (key == "hostname") { _info.Name = value; } else if (key == "gametype") { _info.GameType = value; } else if (key == "game_id") { _info.GameID = value; } else if (key == "version") { _info.Version = value; } else if (key == "plugins") { _info.Plugins = value; } else if (key == "map") { _info.Map = value; } else if (key == "numplayers") { if (!int.TryParse(value, out _info.OnlinePlayers)) { return; } } else if (key == "maxplayers") { if (!int.TryParse(value, out _info.MaxPlayers)) { return; } } else if (key == "hostport") { _info.HostPort = value; } else if (key == "hostip") { _info.HostIP = value; } } data.Skip(1); _info.Players = new List <string>(); while (true) { key = data.Read <string>(); if (key.Length == 0) { break; } _info.Players.Add(key); } _success = true; } else { GetChallenge(); } }