Java和Objective C之间的套接字连接

Java和Objective C之间的套接字连接,java,iphone,objective-c,sockets,Java,Iphone,Objective C,Sockets,我试图用Java编写一个服务器,并使用iPhone应用程序(Objective C)与之通信。连接通过后,我可以向服务器写入数据,但无法从服务器读回数据。我收到一个通知,有数据要读取,但它是空的 Java代码主要取自 目标客户: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point fo

我试图用Java编写一个服务器,并使用iPhone应用程序(Objective C)与之通信。连接通过后,我可以向服务器写入数据,但无法从服务器读回数据。我收到一个通知,有数据要读取,但它是空的

Java代码主要取自

目标客户:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^(void) {
        NSLog(@"staying alive!");
        [[DABMultitaskingController sharedInstance] startTask];
    }];

    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"10.0.1.189", 4444, &readStream, &writeStream);

    NSInputStream *inputStream = (NSInputStream *)readStream;
    NSOutputStream *outputStream = (NSOutputStream *)writeStream;
    [inputStream setDelegate:self];
    [outputStream setDelegate:self];
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [inputStream open];
    [outputStream open];

    _dataToSend = [[NSData dataWithBytes:"This is a test" length:15] retain];

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (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;
        }
        case NSStreamEventHasSpaceAvailable: {
            NSLog(@"can write: %@", theStream);

            if (_dataToSend != nil) {
                NSLog(@"write: %@", _dataToSend);
                [(NSOutputStream *)theStream write:[_dataToSend bytes] maxLength:[_dataToSend length]];
                [_dataToSend release];
                _dataToSend = nil;
            }

            break;
        }
        case NSStreamEventOpenCompleted: {
            NSLog(@"open complete: %@", theStream);

            break;
        }
        case NSStreamEventErrorOccurred: {
            NSLog(@"error occurred: %@", theStream);

            break;
        }
        case NSStreamEventEndEncountered: {
            NSLog(@"end encountered: %@", theStream);

            break;
        }
        default:
            break;
    }
}

从未向java端BufferedReader发送换行符\n使用这些作为readLine()的分隔符


从未向java端BufferedReader发送换行符\n使用这些作为readLine()的分隔符


看起来我需要改为使用
read:maxLength:
。现在工作正常。

看起来我需要使用
读取:maxLength:
。现在工作正常。

没错,但这不是问题的根本原因。
BufferedReader
可以(完全)读取不以行分隔符结尾的数据流。这似乎与只制作自己的缓冲区并使用读取有关。我猜这并没有解决上述问题,但确实使通信更加可靠。这是真的,但不会是问题的根本原因。
BufferedReader
可以(完全)读取不以行分隔符结尾的数据流。似乎只需要制作自己的缓冲区并使用读取,我想这并不能解决上述问题,但它确实使沟通更加可靠。在何处添加此内容,因为它本应编辑您的问题和答案。在何处添加此内容,因为它本应编辑您的问题和答案。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^(void) {
        NSLog(@"staying alive!");
        [[DABMultitaskingController sharedInstance] startTask];
    }];

    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"10.0.1.189", 4444, &readStream, &writeStream);

    NSInputStream *inputStream = (NSInputStream *)readStream;
    NSOutputStream *outputStream = (NSOutputStream *)writeStream;
    [inputStream setDelegate:self];
    [outputStream setDelegate:self];
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [inputStream open];
    [outputStream open];

    _dataToSend = [[NSData dataWithBytes:"This is a test" length:15] retain];

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (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;
        }
        case NSStreamEventHasSpaceAvailable: {
            NSLog(@"can write: %@", theStream);

            if (_dataToSend != nil) {
                NSLog(@"write: %@", _dataToSend);
                [(NSOutputStream *)theStream write:[_dataToSend bytes] maxLength:[_dataToSend length]];
                [_dataToSend release];
                _dataToSend = nil;
            }

            break;
        }
        case NSStreamEventOpenCompleted: {
            NSLog(@"open complete: %@", theStream);

            break;
        }
        case NSStreamEventErrorOccurred: {
            NSLog(@"error occurred: %@", theStream);

            break;
        }
        case NSStreamEventEndEncountered: {
            NSLog(@"end encountered: %@", theStream);

            break;
        }
        default:
            break;
    }
}
_dataToSend = [[NSData dataWithBytes:"This is a test\n" length:16] retain];