Skip to content

Instantly share code, notes, and snippets.

@paulw11
Last active August 29, 2015 14:01
Show Gist options
  • Save paulw11/7321aabef76f2edadc87 to your computer and use it in GitHub Desktop.
Save paulw11/7321aabef76f2edadc87 to your computer and use it in GitHub Desktop.
Example CFStream send
-(void) networkExample
{
uint8_t buf[5];
buf[0]=0xe0;
buf[1]=0x4f;
buf[2]=0xd0;
buf[3]=0x20;
buf[4]=0xea;
NSLog(@"Data=%@ %lu",sendData,sizeof(buf));
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"127.0.0.1", 1234, &readStream, &writeStream);
self.inputStream = (__bridge_transfer NSInputStream *)readStream;
self.outputStream = (__bridge_transfer NSOutputStream *)writeStream;
[self.inputStream setDelegate:self];
[self.outputStream setDelegate:self];
[self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.inputStream open];
[self.outputStream open];
[self.outputStream write:buf maxLength:sizeof(buf)];
}
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
NSLog(@"received event %lu on %@",streamEvent,theStream);
}
@paulw11
Copy link
Author

paulw11 commented May 15, 2014

Test against nc -l 1234 command in a terminal

@paulw11
Copy link
Author

paulw11 commented May 15, 2014

Xcode console log -

2014-05-15 17:50:56.426 CocoaTest[20567:303] Data= 5
2014-05-15 17:50:56.436 CocoaTest[20567:303] received event 1 on <__NSCFOutputStream: 0x608000092890>
2014-05-15 17:50:56.436 CocoaTest[20567:303] received event 4 on <__NSCFOutputStream: 0x608000092890>
2014-05-15 17:50:56.438 CocoaTest[20567:303] received event 1 on <__NSCFInputStream: 0x608000092840>
2014-05-15 17:50:56.438 CocoaTest[20567:303] received event 4 on <__NSCFOutputStream: 0x608000092890>

Terminal output -

$ nc -l 1234 | od -t x1
0000000 e0 4f d0 20 ea
0000005

The Xcode console shows event 1 - stream opened - for both streams and even 4 - space available for the output stream both before and after the write.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment