Hi I have the next issue, why when I send data like this
uint8_t buffer[11] = "I send this";
NSInteger nwritten = [outputStream write:buffer maxLength:sizeof(buffer)];
if (-1 == nwritten) {
NSLog(@"Error writing to stream %@: %@", outputStream, [outputStream streamError]);
}else{
NSLog(@"Wrote %i bytes to stream %@.", nwritten, outputStream);
}
in the other side in the java socket server
connection = new Socket(Constants.HOST, Constants.CHAT_LISTENER_PORT);
_in = new DataInputStream(connection.getInputStream());
String data = _in.readUTF();
But nothing appear like as if it had not sent anything.
I read to much and I found the problem is from platforms because java byte works with big-endians and iOS with little-endians but I don't found information about how do to this.
uint8_t buffer[11] = "I send this"; to big-Endians format
Please heellppp, thanks.
Sorry my English is very but there is nothing in spanish :/ thaks.
On the server side in the next Line in java code:
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.
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;}
At your request string end add \\n
For example uint8_t buffer[] = "I send this\\n"
,it may work.
The swift4 version of the answer.
extension String {
func javaUTF8() -> Data? {
guard let data = self.data(using: .utf8) else {
return nil
}
let length = self.lengthOfBytes(using: .utf8)
var buffer = [UInt8]()
buffer.append(UInt8(0xff & (length >> 8)))
buffer.append(UInt8(0xff & length))
var outdata = Data()
outdata.append(buffer, count: buffer.count)
outdata.append(data)
return outdata
}
}
you can use it, as below.
let data = "hello word".javaUTF8()
readUTF expects a specific format, you need to send the length as a big-endian 16-bit unsigned short, followed by the text UTF encoded. I suspect its taking the first bytes as the length which works out to a large number so it is waiting for this many characters to be read.
UTF-8 is not big endian or little endian, so this is not a problem for you. If you are sending 16-bit, 32-bit or 64-bit data values, then you could have a problem with endianness.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.