我正在尝试让 iPhone 与服务器(在 Java 中)进行通信。我得到了连接,客户端(iPhone)和服务器之间的通信。
现在我正在从服务器向 iPhone 写信:
JAVA代码:
String message = "bye";
byte[] msg = message.getBytes();
OutputStream bufferSalida = client.getOutputStream();
DataOutputStream datos = new DataOutputStream(bufferSalida);
datos.write(msg);
iPhone 代码:
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
switch (streamEvent) {
case NSStreamEventHasBytesAvailable: {
NSLog(@"can read: %@", theStream);
uint8_t *buffer;
NSUInteger length;
[(NSInputStream *)theStream getBuffer:&buffer length:&length];
NSData *data = [NSData dataWithBytes:buffer length:length];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"bytes: %@", data);
NSLog(@"string: %@", string);
break;
}
在日志中,我可以阅读:
2012-05-21 13:03:50.500 sockets[2412:f803] can read: <__NSCFInputStream: 0x6d83e20>
2012-05-21 13:03:50.501 sockets[2412:f803] bytes: <>
2012-05-21 13:03:50.502 sockets[2412:f803] string:
为什么字节和字符串无效???
先感谢您。