user1256477
user1256477

Reputation: 11191

Read a Java server from an iPhone

I'm trying to get an iPhone to communicate with a server (in Java). I got the connection, the communication between the client (iPhone) and the server.

Now I'm writing from the server to the iPhone:

JAVA CODE:

 String message = "bye";

            byte[] msg = message.getBytes();    

            OutputStream bufferSalida = client.getOutputStream();  
            DataOutputStream datos = new DataOutputStream(bufferSalida);                
            datos.write(msg);

iPhone CODE:

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

In the log, I can read:

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:

Why are bytes and string void???

Thank you in advance.

Upvotes: 0

Views: 347

Answers (1)

dda
dda

Reputation: 6203

I am pretty sure you're not reading anything actually. Try and Log the value of your NSUInteger length. See what it says.

Upvotes: 1

Related Questions