Note that there are some explanatory texts on larger screens.

plurals
  1. POSocket connection between Java and Objective C
    primarykey
    data
    text
    <p>I am trying to write a server in Java and communicate with it using an iPhone app (Objective C). The connection goes through, and I can write data to the server, but I can't read data back from the server. I get a notification that there is data to be read, but it's empty.</p> <p>The Java code was taken mostly from <a href="http://download.oracle.com/javase/tutorial/networking/sockets/clientServer.html" rel="nofollow">an Oracle example</a>.</p> <pre><code>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(); </code></pre> <p>And the Objective C client:</p> <pre><code>- (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, &amp;readStream, &amp;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:&amp;buffer length:&amp;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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload