分割UDP数据包
我正在使用 UdpClient 查询游戏服务器有关服务器名称、地图、玩家数量等的信息。
我已遵循此页面上的指南 (A2S_INFO) http://developer.valvesoftware.com/wiki/Server_queries#Source_servers
和我'我得到了正确的回复:
替代文本http://data.fuskbugg.se/skalman01/reply。 JPG
我不知道如何获取每块信息(服务器名称、地图等)。
有什么帮助吗?我假设人们必须查看我链接的维基中指定的回复格式,但我不知道如何理解它。
I'm using UdpClient to query game servers about server name, map, number of players, etc.
I've followed the guidelines on this page (A2S_INFO)
http://developer.valvesoftware.com/wiki/Server_queries#Source_servers
and I'm getting a correct reply:
alt text http://data.fuskbugg.se/skalman01/reply.JPG
I have no idea how I would go about to get each chunk of information (server name, map and the like).
Any help? I'm assuming one would have to look at the reply format specified in the wiki I linked, but I don't know what to make of it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回复格式为您提供回复数据包中字段的顺序和类型,基本上就像一个结构体。您可以使用像
BinaryReader
这样的类来读取数据包中的字节组并将它们解释为适当的数据类型。你如何得到回应?
MemoryStream
中。我认为UdpClient
就是这样做的。然后,使用该流构造一个
BinaryReader
。请记住,流和读取器都需要Dispose
。您现在可以调用读取器的方法,例如
ReadByte
、ReadInt32
等,使用与字段类型相对应的方法从响应中依次读取每个字段。流在读取时更新其内部偏移量,因此您可以自动从响应缓冲区中的正确位置读取连续的字段。BinaryReader
已经具有适合 Steam 数据包中使用的五种非字符串类型的方法:byte: ReadByte
short: ReadInt16
long: ReadInt32
float: ReadSingle
long long: ReadUInt64
(是的,里面有一个 U;Valve 页面说这些是无符号的)string
有点棘手,因为BinaryReader
还没有读取 Valve 指定格式的字符串(以 null 结尾的 UTF-8)的方法,所以你必须自己做,byte按字节。为了使其看起来尽可能像任何其他BinaryReader
方法,您可以编写一个扩展方法(未经测试的代码;这是它的要点):一些示例用法,以及您提供的响应数据包:
您可以使用类似的代码来创建请求数据包,使用
BinaryWriter
而不是手动组装各个字节值。The reply format gives you the order and type of fields in the reply packet, basically like a struct. You can use a class like
BinaryReader
to read groups of bytes in the packet and interpret them as the appropriate data types.How are you getting the response?
MemoryStream
first. I thinkUdpClient
does it this way.Then, construct a
BinaryReader
using the stream. Remember, both the stream and the reader need to beDisposed
of.You can now call the reader's methods like
ReadByte
,ReadInt32
etc. to read each field in turn from the response, using methods corresponding to the fields' types. The stream updates its internal offset as it's read, so you automatically read successive fields from the right place in the response buffer.BinaryReader
already has methods appropriate to the five non-string types used in the Steam packets:byte: ReadByte
short: ReadInt16
long: ReadInt32
float: ReadSingle
long long: ReadUInt64
(yes, there's a U in there; the Valve page says these are unsigned)string
is a bit trickier, becauseBinaryReader
doesn't already have methods to read strings in the format specified by Valve (null-terminated UTF-8), so you'll have to do it yourself, byte by byte. To make it look as much like any otherBinaryReader
method as possible, you could write an extension method (untested code; this is the gist of it):Some example usage, with the response packet you gave:
You can use similar code for creating your request packets, using a
BinaryWriter
instead of manually assembling individual byte values.