private byte[] GetByteFromString(string content) { var output = new List <byte>(); output.AddRange(VarintHelper.IntToVarint(content.Length)); output.AddRange(Encoding.UTF8.GetBytes(content)); return(output.ToArray()); }
private static async Task <ServerStatus> GetStatusCurrent(string ServerAddress, int ServerPort) { try { using (var socket = new TcpClient()) { socket.SendTimeout = 3000; socket.ReceiveTimeout = 3000; socket.ConnectAsync(ServerAddress, ServerPort).Wait(3000); BinaryWriter writer; #region handshake var handshakeStream = new MemoryStream(); var handshakewriter = new BinaryWriter(handshakeStream); handshakewriter.Write((byte)0x00); // Packet ID // Protocol version, http://wiki.vg/Protocol_version_numbers handshakewriter.Write(VarintHelper.IntToVarint(210)); handshakewriter.Write(GetByteFromString(ServerAddress)); // hostname or IP handshakewriter.Write((short)ServerPort); // Port handshakewriter.Write(VarintHelper.IntToVarint(0x01)); // Next state, 1 for `status' handshakewriter.Flush(); writer = new BinaryWriter(socket.GetStream()); writer.Write(VarintHelper.IntToVarint((int)handshakeStream.Length)); writer.Write(handshakeStream.ToArray()); writer.Flush(); #endregion writer = new BinaryWriter(socket.GetStream()); /* BE: 0x0100, Length and writer.Write((byte)0x00); * ID for `Request' */ writer.Write((short)0x0001); writer.Flush(); var streamIn = socket.GetStream(); var reader = new BinaryReader(streamIn); var packetLen = VarintHelper.ReadVarInt(reader); var packetId = VarintHelper.ReadVarInt(reader); var packetJsonLen = VarintHelper.ReadVarInt(reader); var response = reader.ReadBytes(packetJsonLen); var json = Encoding.UTF8.GetString(response); Console.WriteLine(json); return(JsonConvert.DeserializeObject <ServerStatus>(json)); } } catch (Exception e) { Console.WriteLine(e); return(null); } }