将数据包发送到Minecraft服务器:目标c [英] Sending packets to Minecraft server: Objective c
问题描述
我一直在尝试从我的自定义Cocoa应用程序(当然是用Objective-C编写)将数据包发送到Minecraft服务器.我对如何做到这一点有些困惑.我用Java完成的.那很容易.这样做是客观的,尽管事实证明这更具挑战性.
I've been trying to send packets to a minecraft server from my custom Cocoa application (written in objective-c of course). I am a little confused as how to do that though. I did it in Java. That was very easy. Doing this is objective-c though is proving to be a bit more challenging.
这是我正在使用的代码:
This is the code that I am using:
- (void)handshake
{
PacketHandshake *packet = [PacketHandshake packetString:[NSString stringWithFormat:@"%@;%@:%i", username, IP, PORT]];
[packet writeData:dataOut];
}
哪个电话:
- (void)writeData:(NSOutputStream *)dataOut
{
[super writeData:dataOut]; //Writes the "header" which is a char with the value of 0x02 (char packetID = 0x02)
NSUInteger len = [string lengthOfBytesUsingEncoding:NSUTF16BigEndianStringEncoding]; //Getting the length of the string i guess?
NSData *data = [string dataUsingEncoding:NSUTF16BigEndianStringEncoding]; //Getting string bytes?
[dataOut write:(uint8_t*)len maxLength:2]; //Send the length?
[dataOut write:[data bytes] maxLength:[data length]]; //Send the actual string?
}
我已经预先建立了到服务器的成功连接,但是我真的不知道我是否正确发送了数据包.有人可以解释一下我应该如何发送各种数据类型和对象. (int,byte/char,short,double,NSString,BOOL/bool)
I have established a successful connection to the server beforehand, but I don't really know whether or not I am sending the packets correctly. Could somebody please explain how I should send various data types and objects. (int, byte/char, short, double, NSString, BOOL/bool)
还有,有没有像Minecraft要求的那样发送数据包的特定或通用方法?
Also, is there any specific or universal way to send packets like the ones required by Minecraft?
好吧,我想现在的问题是:Java和Objective-C中的数据类型(主要是字符串)如何关联?
Ok, I guess the question is now: how do data types, mainly strings, relate in Java and Objective-C?
感谢您的帮助,谢谢!
没人知道吗?
推荐答案
也许您遇到了网络/主机字节顺序问题?我对Minecraft知之甚少,但我注意到提到的此处是Minecraft协议中使用的简称网络字节顺序,即big-endian(其他所有数据类型均为1字节长,因此endianness不相关). 所有x86机器都使用little-endian.
Maybe you're running into a network/host byte order problem? I know very little about Minecraft- but I note that it's mentioned here that shorts in the Minecraft protocol use network byte order, which is big-endian (all other data types are 1 byte long so endianness is not relevant). All x86 machines use little-endian.
我不知道您的PacketHandshake类在发送数据之前是否正在转换数据-否则,您可以使用c库函数ntohs()
和htons()
,为此您需要包括sys/types.h和
netinet/in.h
该链接还提到字符串是标准ASCII字符的64字节数组,并用0x20s填充.您可以通过执行[string UTF8String]
从NSString中获取ASCII值,它返回const char*
-即您的标准C字符串以0x0结尾,然后可以填充它.但是,如果它仅适用于Java,则也许您不需要.
I don't know whether your PacketHandshake class is converting the data before sending it- if not you could use the c library functions ntohs()
and htons()
, for which you'd need to include sys/types.h and
netinet/in.h
The link also mentions that strings are 64 byte array of standard ASCII chars, padded with 0x20s. You can get the ASCII value out of an NSString by doing [string UTF8String]
, which returns const char*
- i.e. your standard C String ending with a 0x0, and then maybe pad it. But if it just works in Java, then maybe you don't need to.
这篇关于将数据包发送到Minecraft服务器:目标c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!