我正在使用UdpClient来查询游戏服务器有关服务器名称,地图,玩家数量等的信息.
我已按照此页面上的指南(A2S_INFO) http://developer.valvesoftware.com/wiki/Server_queries#Source_servers
我收到了正确答复:
替代文字http://data.fuskbugg.se/skalman01/reply.JPG
我不知道如何获取每一块信息(服务器名称,地图等).
有帮助吗?我假设我必须查看我链接的wiki中指定的回复格式,但我不知道该怎么做.
回复格式为您提供回复数据包中字段的顺序和类型,基本上类似于结构.您可以使用类BinaryReader来读取数据包中的字节组,并将它们解释为适当的数据类型.
你是如何得到回应的?
MemoryStream第一个数组中.我认为UdpClient这样做.然后,BinaryReader使用流构造一个.请记住,流和读者都需要Disposed.
BinaryReader reader = new BinaryReader(stream);
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用与字段类型对应的方法ReadByte,调用读者的方法等,ReadInt32从响应中依次读取每个字段.流在读取时更新其内部偏移量,因此您可以自动从响应缓冲区中的正确位置读取连续字段. BinaryReader已经有适合Steam数据包中使用的五种非字符串类型的方法:
byte: ReadByteshort: ReadInt16long: ReadInt32float: ReadSinglelong long: ReadUInt64 (是的,那里有一个U; Valve页面说这些是未签名的)string有点棘手,因为BinaryReader还没有方法以Valve指定的格式读取字符串(以null结尾的UTF-8),所以你必须自己逐个字节地完成.为了使它看起来像任何其他BinaryReader方法一样,你可以写一个扩展方法(未经测试的代码;这是它的要点):
public static string ReadSteamString(this BinaryReader reader)
{
// To hold the list of bytes making up the string
List<byte> str = new List<byte>();
byte nextByte = reader.ReadByte();
// Read up to and including the null terminator...
while (nextByte != 0)
{
// ...but don't include it in the string
str.Add(nextByte);
nextByte = reader.ReadByte();
}
// Interpret the result as a UTF-8 sequence
return Encoding.UTF8.GetString(str.ToArray());
}
Run Code Online (Sandbox Code Playgroud)
一些示例用法,包含您提供的响应数据包:
// Returns -1, corresponding to FF FF FF FF
int header = reader.ReadInt32();
// Returns 0x49, for packet type
byte packetType = reader.ReadByte();
// Returns 15, for version
byte ver = reader.ReadByte();
// Returns "Lokalen TF2 #03 All maps | Vanilla"
string serverName = reader.ReadSteamString();
// Returns "cp_well"
string mapName = reader.ReadSteamString();
// etc.
Run Code Online (Sandbox Code Playgroud)
您可以使用类似的代码创建请求数据包,BinaryWriter而不是手动汇编单个字节值.
| 归档时间: |
|
| 查看次数: |
2028 次 |
| 最近记录: |