【发布时间】:2013-03-05 02:31:06
【问题描述】:
我正在尝试为我的世界服务器制作客户端/机器人,以自动保护聊天并禁止垃圾邮件发送者。 (第一个成就)
我在这里找到了一些文档,我已经实现了数据类型from here(so they look like this - 我还没有完成)。现在,我正在尝试发送初始0x02 数据包,它应该如下所示:
我的数据包格式
size value comment
1 0x02 Packet ID
2+? string Username (I send "jakub")
2+? another string Server host name - here, the program FAILS*
4 25565 Port number
*Fails 表示 bukkit 服务器在控制台输出以下内容,socket 关闭:
11:09:45 [INFO] /127.0.0.1:51256 失去连接
我现在可以看到,如何测试我的数据类型是否正确,但因为我似乎发送用户名没有问题,我认为我发送错误信息,虽然在 正确的格式。
生成字符串
但无论如何,我很好奇我是否做对了一切。我有 mc_short 和 mc_string 的课程。这就是mc_short 自己创建 2 个字节的方式:
//mc_short::val is type of short
void mc_short::asBytes(char* data) {
for (int i = 0; i < 2; i++)
data[endianity?i:1-i] = (val >> (i * 8)); //Some magic with byteshifting.
//endianity is set to false, because java uses BigEndian everywhere** (UNFRIENDLY PERSONS!!)
}
**参考http://wiki.vg/Data_Types:
Java(以及我的世界)中的所有类型都是大端的,也就是说,最重要的字节在前。
然后字符串本身使用mc_short 和std::string 来填充char* 中的字节。
void mc_string::asBytes(char* data) {
mc_short size((short)val.length()); //val is std::string
size.endianity = endianity; //mc_string::endianity is boolean, and is ony used to determine endianity of the first 2 bytes
size.asBytes(data); //Filling 2 bytes in data - length info
for(short i=0; i<size.value(); i++) {
data[i+2] = val[i]; //Copying std::string to data
}
}
问题总结
- 我应该发送什么作为“服务器主机名”,0x02 的第三个字段?
- 我发送的字符串是否正确?不需要任何其他转换吗?
我知道,我的问题是关于不太为人所知的主题,因此您可能不知道也不知道答案,也不知道主题本身。在这种情况下,请忽略这个问题。
【问题讨论】: