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

时间:2011-05-29 01:54:00

标签: java iphone objective-c sockets

我正在尝试用Java编写服务器并使用iPhone应用程序(Objective C)与它进行通信。连接通过,我可以将数据写入服务器,但我无法从服务器读取数据。我收到一条通知,告知有数据需要读取,但它是空的。

Java代码主要来自an Oracle example

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine;

out.println("Hello World");

while ((inputLine = in.readLine()) != null) {
    System.out.println(inputLine);
    System.out.flush();

    out.println("test");

    if (inputLine.equals("Bye")) {
        break;
    }
}

out.close();
in.close();
socket.close();

Objective C客户端:

- (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;
    }
}

2 个答案:

答案 0 :(得分:1)

你永远不会向java方面发送换行符\ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n>它们作为readLine();

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

答案 1 :(得分:0)

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