Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NSOutputStream's write message?

I want to send a UIImage to a server through a socket.

a)I open the outputstream:


- (IBAction)send:(id)sender {
    NSURL *website = [NSURL URLWithString:str_IP];
    NSHost *host = [NSHost hostWithName:[website host]];
    [NSStream getStreamsToHost:host port:1100 inputStream:nil outputStream:&oStream];
    [oStream retain];
    [oStream setDelegate:self];
    [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [oStream open];
}

b) I write NSData to outputstream after open completed and handle the error if error occurs.


- (void) stream: (NSStream *) stream handleEvent: (NSStreamEvent) eventCode
{
    //printf("EVENT: Start.\n");

    switch(eventCode)
    {
            case NSStreamEventOpenCompleted:
            {
                //printf("EVENT: Open completed.\n");

                if(stream == oStream)
                    {
                        //printf("Sending...\n");
                        NSData *data = UIImageJPEGRepresentation(drawImage.image, 90);
                        NSInteger x = [oStream write:[data bytes] maxLength:[data length]];
                    }

                break;
            }
            case NSStreamEventEndEncountered:
            {
                //printf("EVENT: End encountered.\n");
                break;
            }
            case NSStreamEventHasSpaceAvailable:
            {
                //printf("EVENT: Has space available.\n");
                break;
            }
            case NSStreamEventHasBytesAvailable:
            {
                //printf("EVENT: Has bytes available.\n");
                break;
            }
            case NSStreamEventErrorOccurred:
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error Occurred"       
                                                                message:nil 
                                                               delegate:nil             
                                                      cancelButtonTitle:@"OK"               
                                                      otherButtonTitles:nil];                   
                [alert show];
                [alert release];
                break;
            }
            case NSStreamEventNone:
            {
                //printf("EVENT: None.\n");
                break;
            }
    }

    //printf("EVENT: End.\n");
}

When I run this code, NSStreamEventOpenCompleted and NSStreamEventErrorOccurred is called.The NSOutputStream's write method was called successfully and all the data is not nil. But after the data is written to the oStream, the eventCode will change to NSStreamEventErrorOccurred.

So I think maybe it's not the correct way to use [oStream write]. What's the correct way to use this message then? I find this message returns an NSInteger of -1073748088, what might be the problem?

like image 471
Chilly Zhong Avatar asked Apr 01 '09 01:04

Chilly Zhong


1 Answers

You should be writing the data only after space becomes available in the output stream. When the stream finishes opening, it doesn't always immediately have space available, so writing to it won't work. If you move the write call to the NSStreamEventHasSpaceAvailable handler, it should succeed.

Also, the computer on the other end of the socket has no way of knowing the length of the data you're sending. Unless you're signaling the end-of-data by closing the socket, you should explicitly send the data length along with the data:

case NSStreamEventHasSpaceAvailable:
{
    if(stream == oStream)
    {
        NSData *data = UIImageJPEGRepresentation(drawImage.image, 90);
        // Convert from host to network endianness
        uint32_t length = (uint32_t)htonl([data length]);
        // Don't forget to check the return value of 'write'
        [oStream write:(uint8_t *)&length maxLength:4];
        [oStream write:[data bytes] maxLength:length];
    }
    break;
}
like image 129
Adam Rosenfield Avatar answered Nov 13 '22 04:11

Adam Rosenfield