Not getting more messages after receiving first message from Java server in iOS client using NSInputStream

337 views Asked by At

I've got a Java server (and it's able to correctly read a request from my iOS client -- it even generates a response and appears to send it correctly, though I got First message response from server every time but not getting other messages after receiving first message):

sequence of communication

Step 1-> client send login message to server

Step 2-> server validate the user and sends login info to the clients

Step 3-> Message Packet1

         Message Packet2

         Message Packet3

         Message Packet4

 step-4-> I have checked server log and it says server has send 4 string 
          messages

 Step-5 -> On client side I am receiving only first message i.e. Message 
           Packet1, and there no other packets on NSInputStream showing. or NSStreamEventHasBytesAvailable option in delegate method->
- (void)stream:(NSStream *)theStream
handleEvent:(NSStreamEvent)streamEvent

Not calling most of the time more than one but sometimes It calls and gets MessagePacket2 or MessagePacket4 data.

Please help me out, I am unable to figure out why I am receiving only first packet from server instead of 4 packets, as server sending 4 packets.

I have used code from the below tutorial -> http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server#comments

My code is here->

@interface NetworkManager()<NSStreamDelegate>

@property (strong, nonatomic)NSInputStream        *objInputStream;
@property (strong, nonatomic)NSOutputStream       *objOutputStream;

@end

- (void)initializeNetworkCommunicationToServer
{
self.networkOpened = NO;

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;

CFStreamCreatePairWithSocketToHost(NULL,
                                   (CFStringRef)SERVER_HOSTNAME,
                                   SERVER_PORT_ADDR,
                                   &readStream,
                                   &writeStream);

self.objInputStream = (__bridge_transfer NSInputStream *)readStream;
self.objOutputStream = (__bridge_transfer NSOutputStream*)writeStream;


[self.objInputStream setDelegate:self];
[self.objOutputStream setDelegate:self];

[self.objInputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                               forMode:NSDefaultRunLoopMode];
[self.objOutputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                                forMode:NSDefaultRunLoopMode];
[self.objInputStream open];
[self.objOutputStream open];

}

//------------------------------------------------------
pragma NSStreamDelegate delegate method
//------------------------------------------------------

- (void)stream:(NSStream *)theStream
handleEvent:(NSStreamEvent)streamEvent
{

switch (streamEvent)
{
    case NSStreamEventNone:
    {
        NSLog(@"NSStreamEventNone");
        break;
    }
    case NSStreamEventOpenCompleted:
    {
        NSLog(@"NSStreamEventOpenCompleted");

    }
        break;
    case NSStreamEventHasBytesAvailable:
    {
        NSLog(@"NSStreamEventHasBytesAvailable:");

        if (theStream == self.objInputStream)
        {
            while ([self.objInputStream hasBytesAvailable])
            {
                uint8_t buffer[1024];
                unsigned int len = 0;

                len = [self.objInputStream read:buffer
                                      maxLength:sizeof(buffer)];
                if (len > 0)
                {

                    NSString *output =
                    [[NSString alloc] initWithBytes:buffer
                                             length:len
                                           encoding:NSASCIIStringEncoding];

                    if (nil != output)
                    {
                        NSLog(@"server said: %@", output);
                    }

                }//end of if(len > 0)

            }//end of while
        } //end of  if (theStream == self.objInputStream)

    }
        break;
    case NSStreamEventErrorOccurred:
    {
        NSLog(@"NSStreamEventErrorOccurred: Can not connect to the host!");
    }
        break;
    case NSStreamEventEndEncountered:
    {

        NSLog(@"NSStreamEventEndEncountered & network connection ended");

        [theStream close];
        [theStream removeFromRunLoop:[NSRunLoop currentRunLoop]
                             forMode:NSDefaultRunLoopMode];

        theStream = nil;
    }
        break;

        // The NSStreamEventHasSpaceAvailable event indicates that you can write (at least one byte!) to the stream without blocking. That does not mean that previously written data is completely delivered to the other endpoint of the connection.
    case NSStreamEventHasSpaceAvailable:
    {
        NSLog(@"NSStreamEventHasSpaceAvailable");

        if(NO == self.networkOpened)
        {
            self.networkOpened = YES;

            [self sendMessage:@"login:username,password"];

        }
    }
        break;
    default:
    {
        NSLog(@"Unknown event");
    }
}
}


//------------------------------------------------------
#pragma mark - send packet
//------------------------------------------------------

- (void)sendMessage:(NSString*)lstrMessage
{
NSMutableData *data = [[NSMutableData alloc] initWithData:
                       [lstrMessage dataUsingEncoding:NSASCIIStringEncoding]];

unsigned char suffixBytes[] = {1, 1, 0};
[data appendBytes:suffixBytes length:3];

[self.objOutputStream write:[data bytes] maxLength:[data length]];

NSLog(@"message sent->%@",[NSString stringWithUTF8String:[data bytes]]);

}
1

There are 1 answers

0
Pavel Osipov On

I have no idea what is wrong with your code, but my experience with streams tuning tells me that there will be make sence to try another way to subscribe to Core Foundation streams using CF API instead of toll-free-bridging it to NSInputStream. I mean CFReadStreamSetClient and CFReadStreamScheduleWithRunLoop functions. You can see example how to do so in my helper class for testing POSInputStreamLibrary.