+2  A: 

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;
}
Adam Rosenfield
iPhoney
I've updated the code -- just cast to a uint8_t* to get rid of the warning. (This technically violates the C99 strict aliasing rules, but in practice this won't matter.)
Adam Rosenfield
The oStream now keeps sending the length and data to the server. The server still doesn't know the end of the data. Where can I add [oStream close];? Or should I add code to the server side to close it?
iPhoney
Have the server read in the length, then read in that many bytes of data, then close the connection (or keep it open if you'll be sending more data).
Adam Rosenfield
A: 

And how do you do if you want to keep the socket open, and then write to it later???

Ptitaw
Just don't close it before you have written everything to it.
iPhoney
This is not an answer.
Shaggy Frog