title: iPhone stream programming (CFStream) Hello World
body: <p>I am currently trying to send a Hello World from my iPhone to a remote computer running a working server (tested by telnet on iPhone).</p>
  
  <p>Here is my code :</p>
  
  <pre><code>#import "client.h"
  
  @implementation client
  
  - (client*) client:init {
   self = [super init];
   [self connect];
   return self;
  }
  
  - (void)connect {
          CFWriteStreamRef writeStream;
          CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[NSString stringWithFormat: @"192.168.1.1"], 50007, NULL, &amp;writeStream);
    NSLog(@"Creating and opening NSOutputStream...");
    oStream = (NSOutputStream *)writeStream;
    [oStream setDelegate:self];
    [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [oStream open];
  }
  
  - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
      NSLog(@"stream:handleEvent: is invoked...");
  
      switch(eventCode) {
          case NSStreamEventHasSpaceAvailable:
          {
              if (stream == oStream) {
                  NSString * str = [NSString stringWithFormat: @"Hello World"];
                  const uint8_t * rawstring =
      (const uint8_t *)[str UTF8String];
                  [oStream write:rawstring maxLength:strlen(rawstring)];
                  [oStream close];
              }
              break;
          }
      }
  }
  
  @end
  </code></pre>
  
  <p>For the client.h :</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  
  @interface client : NSObject {
   NSOutputStream *oStream;
  }
  
  -(void)connect;
  
  @end
  </code></pre>
  
  <p>Finally, in the AppDelegate.m :</p>
  
  <pre><code>- (void)applicationDidFinishLaunching:(UIApplication *)application {    
  
      // Override point for customization after app launch    
      [window addSubview:viewController.view];
   [window makeKeyAndVisible];
   [client new];
  }
  </code></pre>
  
  <p>Does someone has any idea of what is going wrong?</p>