-
-
Save rjungemann/446256 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h> | |
@interface Communicator : NSObject <NSStreamDelegate> { | |
@public | |
NSString *host; | |
int port; | |
} | |
- (void)setup; | |
- (void)open; | |
- (void)close; | |
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)event; | |
- (void)readIn:(NSString *)s; | |
- (void)writeOut:(NSString *)s; | |
@end |
#import "Communicator.h" | |
CFReadStreamRef readStream; | |
CFWriteStreamRef writeStream; | |
NSInputStream *inputStream; | |
NSOutputStream *outputStream; | |
@implementation Communicator | |
- (void)setup { | |
NSURL *url = [NSURL URLWithString:host]; | |
NSLog(@"Setting up connection to %@ : %i", [url absoluteString], port); | |
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)[url host], port, &readStream, &writeStream); | |
if(!CFWriteStreamOpen(writeStream)) { | |
NSLog(@"Error, writeStream not open"); | |
return; | |
} | |
[self open]; | |
NSLog(@"Status of outputStream: %i", [outputStream streamStatus]); | |
return; | |
} | |
- (void)open { | |
NSLog(@"Opening streams."); | |
inputStream = (NSInputStream *)readStream; | |
outputStream = (NSOutputStream *)writeStream; | |
[inputStream retain]; | |
[outputStream retain]; | |
[inputStream setDelegate:self]; | |
[outputStream setDelegate:self]; | |
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; | |
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; | |
[inputStream open]; | |
[outputStream open]; | |
} | |
- (void)close { | |
NSLog(@"Closing streams."); | |
[inputStream close]; | |
[outputStream close]; | |
[inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; | |
[outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; | |
[inputStream setDelegate:nil]; | |
[outputStream setDelegate:nil]; | |
[inputStream release]; | |
[outputStream release]; | |
inputStream = nil; | |
outputStream = nil; | |
} | |
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)event { | |
NSLog(@"Stream triggered."); | |
switch(event) { | |
case NSStreamEventHasSpaceAvailable: { | |
if(stream == outputStream) { | |
NSLog(@"outputStream is ready."); | |
} | |
break; | |
} | |
case NSStreamEventHasBytesAvailable: { | |
if(stream == inputStream) { | |
NSLog(@"inputStream is ready."); | |
uint8_t buf[1024]; | |
unsigned int len = 0; | |
len = [inputStream read:buf maxLength:1024]; | |
if(len > 0) { | |
NSMutableData* data=[[NSMutableData alloc] initWithLength:0]; | |
[data appendBytes: (const void *)buf length:len]; | |
NSString *s = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; | |
[self readIn:s]; | |
[data release]; | |
} | |
} | |
break; | |
} | |
default: { | |
NSLog(@"Stream is sending an Event: %i", event); | |
break; | |
} | |
} | |
} | |
- (void)readIn:(NSString *)s { | |
NSLog(@"Reading in the following:"); | |
NSLog(@"%@", s); | |
} | |
- (void)writeOut:(NSString *)s { | |
uint8_t *buf = (uint8_t *)[s UTF8String]; | |
[outputStream write:buf maxLength:strlen((char *)buf)]; | |
NSLog(@"Writing out the following:"); | |
NSLog(@"%@", s); | |
} | |
@end |
#import <Foundation/Foundation.h> | |
#import "Communicator.h" | |
int main (int argc, const char * argv[]) { | |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; | |
Communicator *c = [[Communicator alloc] init]; | |
c->host = @"http://127.0.0.1"; | |
c->port = 6789; | |
[c setup]; | |
[c open]; | |
[pool drain]; | |
return 0; | |
} |
hi @rjungemann
Thank you for simple code. Could you help me write something similar in Swift 3.0
Amazing example works well.... I am able to send data to other device but when i write to output stream data is getting received on other side when i close my app, please help what could be the mistake?
Not able to send data in ios 11
Code is working well means I am getting streaming data in handleEvent delegate method but when app is in foreground mean when I lock my iPhone the connection getting break TCP connection break. So how I can handle this problem. I got stuck on this point.
Thanks
[data appendBytes: (const void *)buf length:len];
I got crash here
For those who successfully streamed, what is the suitable stream and event argument to pass in in order to start Listening?
Hi,
I am able to open a stream with the server ,but how can i write a message in the output stream ?
I am sorry but I am completely new to objective C.
Any help would be appreciated
I want to write any message-May be "hello World"