简体   繁体   English

将数据从iOS发送到Java套接字服务器并识别该消息

[英]Sending Data from iOS to java socket server and recognizes the message

I don't understand this, I do this code in my app and in the server side don't recognized well the message why? 我不明白这一点,我在我的应用程序中执行此代码并且在服务器端不能很好地识别消息为什么?

NSString *init = @"?II#$%_0";
NSData *myData = [init dataUsingEncoding:NSUTF16BigEndianStringEncoding];
uint8_t *readBytes = (uint8_t *)[myData bytes];
int size = [myData length]; 
int myInteger = CFSwapInt16HostToBig(size);
NSInteger nwritten = [outputStream write:readBytes maxLength:myInteger];

And in the other side in the remote server recognizes the message but not well like this. 而在远程服务器的另一端识别消息,但不是这样。

0I0I0#0$0%0_0O0000000000000000#0000000000000000

this have the II # $ _ 0 from the string message what I'm sending but with 000000 and this 000 is not cero is a square but I did not know how to write this character. 这个字符串消息中的II#$ _ 0是我发送的但是000000这个000不是cero是一个正方形但是我不知道怎么写这个字符。

and next the error 然后是错误

java.io.UTFDataFormatException: malformed input around byte 27 at java.io.DataInputStream.readUTF(DataInputStream.java:639) at java.io.DataInputStream.readUTF(DataInputStream.java:547)

I don't understand wh?? 我不明白wh ?? please heelp sorry for my english is very bad. 请抱歉抱歉我的英语非常糟糕。 :/ :/

Sorry for my Englis is very bad. 对不起,我的英格利斯非常糟糕。 :/ Finally I found the solution to the headache hahahaha, yeaaa :) :/最后我找到了头痛的解决方法hahahaha,yeaaa :)

In the side of the server in the next Line in java code: 在java代码中的下一行中的服务器端:

DataInputStream.readUTF();

this .readUTF() expect a UTF java native type what is not the same with objective-c type, and the solution was send the string codifying in UTF native java like this. 这个.readUTF()期望UTF java本机类型与objective-c类型不一样,解决方案是发送字符串编码在UTF本机java这样。

NSString *msg = @"initChat_";
NSString *messageToSend  = [NSString stringWithFormat:@"%@", msg];
NSData *data = [self convertToJavaUTF8:messageToSend];

int dataLenght = [data length];

int num = [outputStream write:(const uint8_t *)[data bytes] maxLength:dataLenght];


if (-1 == num) {
    NSLog(@"Error writing to stream %@: %@", outputStream, [outputStream streamError]);
}else{
    NSLog(@"Wrote %i bytes to stream %@.", num, outputStream);
}

And the Magic come from: 而魔术来自:

- (NSData*) convertToJavaUTF8 : (NSString*) str {
NSUInteger len = [str lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
Byte buffer[2];    
buffer[0] = (0xff & (len >> 8));
buffer[1] = (0xff & len);
NSMutableData *outData = [NSMutableData dataWithCapacity:2];
[outData appendBytes:buffer length:2];        
[outData appendData:[str dataUsingEncoding:NSUTF8StringEncoding]];
return outData;}

Cheers..... 干杯.....

DataInputStream.readUTF() reads data that has been encoded in the special prefixed format produced by, and described in the Javadoc for, DataOutputStream.writeUTF() . DataInputStream.readUTF()读取已使用Javadoc for DataOutputStream.writeUTF()生成的特殊前缀格式编码的数据。 It's not a generic method for reading UTF strings. 它不是读取UTF字符串的通用方法。 Use a BufferedReader with an appropriate charset. 使用带有适当字符集的BufferedReader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM