0

I am developing iOS app which required to send message to Android Socket server.

I am facing a weird situation. If I continuously write data in NSOutputStream then Android device get Message but, If I write code into While loop or any If condition then it is not working.

 - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode;{
    switch (eventCode) {

        case NSStreamEventOpenCompleted:{
            NSLog(@"Stream opened");
        }
            break;

        case NSStreamEventHasBytesAvailable:
            NSLog(@"Stream has bytes available");

            break;

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

        case NSStreamEventEndEncountered:
            NSLog(@"Stream closed");

            break;
        case NSStreamEventHasSpaceAvailable:{

//            if (sendCount < 100){ //NOT WORKING
                uint8_t *readBytes = (uint8_t *)[_data mutableBytes];
                readBytes += byteIndex; // instance variable to move pointer
                int data_len = [_data length];
                unsigned int len = ((data_len - byteIndex >= 1024) ?
                                    1024 : (data_len-byteIndex));
                uint8_t buf[len];
                (void)memcpy(buf, readBytes, len);
                len = [outputStream write:(const uint8_t *)buf maxLength:len];
                //            byteIndex += len;
                byteIndex = 0;
                sendCount++;
//            }
        }
            break;

        default:

            NSLog(@"Unknown event: %@ : %lu", aStream, (unsigned long)eventCode);
    }
}

In above code, it continuously write into NSOutputstream and Android device get data. But If I uncomment line with If condition then it will not work. Any help will be appreciated.

girish_pro
  • 838
  • 9
  • 18
  • 1
    You appear to have copied that code from an [Apple example](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Streams/Articles/WritingOutputStreams.html) however you have set `byteIndex = 0` which probably explains the *tight-loop* behaviour you are probably experiencing. – trojanfoe Mar 16 '16 at 12:29
  • I set byteIndex = 0 because I want to send whole data from starting index. this is just for testing. – girish_pro Mar 16 '16 at 12:35
  • But that's not how the eventing system will work. You attempt to write as much as possible and `write:maxLength:` will tell you how much it managed to write so you can control `byteIndex`. You are messing that system up. Also I don't see the need for `buf` and `memcpy()` either as you can write from `readBytes` (i.e. a pointer into the `NSData` object's data). – trojanfoe Mar 16 '16 at 12:38
  • I had testing with Android developer. if we continuously send data at that time, he is able to get. – girish_pro Mar 16 '16 at 13:02
  • @trojanfoe : why stream can be available for read when it is sending too much data of repeated data continuously? Can you please guide us? – girish_pro Mar 16 '16 at 13:09

0 Answers0