3

I am learning iOS network programming from this tutorial. I tried modifying the code so that a response is sent to the server immediately after a connection is successful. The only part of the code I change is in this function. The problem is that the app stalls and nothing happens on the line [outputStream write:[data bytes] maxLength:[data length]]; Thus NSLog(@"sent test"); does not get executed. What am I doing wrong?

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {       
    NSLog(@"stream event %i", streamEvent);
    switch (streamEvent) {
        case NSStreamEventOpenCompleted:
            NSLog(@"Stream opened");

            //my code
            if (theStream == outputStream) {
                NSLog(@"outputStream");                                
                NSData* data = [NSData dataWithBytes:@"test" length:4];
                [outputStream write:[data bytes] maxLength:[data length]];
                NSLog(@"sent test");
            } //end my code

            break;
        case NSStreamEventHasBytesAvailable:
            if (theStream == inputStream) {
                uint8_t buffer[1024];
                int len;
                while ([inputStream hasBytesAvailable]) {
                    len = [inputStream read:buffer maxLength:sizeof(buffer)];
                    //...
                }
            }
            break;
        case NSStreamEventErrorOccurred:
            NSLog(@"Can not connect to the host!");
            break;
        case NSStreamEventEndEncountered:
            [theStream close];
            [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            [theStream release];
            theStream = nil;
            break;
        default:
            NSLog(@"Unknown event");
    }
}

EDIT: solution found here How to use NSOutputStream's write message?

Community
  • 1
  • 1
user299648
  • 2,769
  • 6
  • 34
  • 43
  • I'm not sure if this is the problem but the line `NSData* data = [NSData dataWithBytes:@"test" length:4];` is not correct. It should be: `NSData *data = [@"test" dataUsingEncoding:NSUTF8StringEncoding];`. – rmaddy Mar 19 '13 at 05:07
  • Thanks for the tip. I tried it, but it doesn't solve my problem. Maybe you can download the tutorial app and add my snippet of code and try running it. – user299648 Mar 19 '13 at 05:11
  • I'm not an expert on using `NSStreams` for socket programming (frankly I prefer the raw BSD socket C API), but according to [Apple's programming guide](https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Streams/Articles/WritingOutputStreams.html), you should be writing your data in the `NSStreamEventHasSpaceAvailable` event handler, which gets called whenever you can write data to the socket without blocking (you don't want to block your UI thread while you perform socket operations). – Adam Rosenfield Mar 19 '13 at 05:22
  • Is there any drawbacks to using BSD sockets over NSStream in iOS? – user299648 Mar 19 '13 at 05:34

1 Answers1

1

ok while i was working with raw socket connections i have used that code to send data to server. it might be helpful to you

if (theStream == outputStream) {
        NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];//str is my string to send
        int byteIndex = 0;
        uint8_t *readBytes = (uint8_t *)[data bytes];
        readBytes += byteIndex; // instance variable to move pointer
        int data_len = [data length];
       // NSLog(@"%i",[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;

}
meth
  • 1,887
  • 2
  • 18
  • 33
  • Did you put the code immediately after the event NSStreamEventOpenCompleted or somewhere else? Because I tried replacing your code with mines at the same spot. It does not fix the stall. – user299648 Mar 19 '13 at 17:56
  • it was in case NSStreamEventHasSpaceAvailable: part – meth Mar 19 '13 at 18:04
  • snippet you provided works fine. but outputstream does not send data with high speed i.e. stream is not utilizing bandwidth available for my wifi network. I'm working on upload speed of about 40 mbps but uploading file using above code uses only around 4-5 mbps speed. – K_Mohit Nov 16 '15 at 13:04