Solution for people following the connection method:
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef) urlStr, portNo, &readStream, &writeStream); if (readStream && writeStream) { CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue); CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue); iStream = (NSInputStream *)readStream; [iStream retain]; [iStream setDelegate:self]; [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [iStream open]; oStream = (NSOutputStream *)writeStream; [oStream retain]; [oStream setDelegate:self]; [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [oStream open]; }
uses
-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
like this:
-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent { NSString *io; if (theStream == iStream) io = @">>"; else io = @"<<"; NSString *event; switch (streamEvent) { case NSStreamEventNone: event = @"NSStreamEventNone"; statusText.text = @"Can not connect to the host!"; break; case NSStreamEventOpenCompleted: event = @"NSStreamEventOpenCompleted"; pingButton.hidden = NO; statusText.text = @"Connected"; break; case NSStreamEventHasBytesAvailable: event = @"NSStreamEventHasBytesAvailable"; if (theStream == iStream) { //read data uint8_t buffer[1024]; int len; while ([iStream hasBytesAvailable]) { len = [iStream read:buffer maxLength:sizeof(buffer)]; if (len > 0) { NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]; NSData *theData = [[NSData alloc] initWithBytes:buffer length:len]; if (nil != output) { //do something with data } } } } break; case NSStreamEventHasSpaceAvailable: event = @"NSStreamEventHasSpaceAvailable"; if (theStream == oStream) { //send data uint8_t buffer[11] = "I send this"; int len; len = [oStream write:buffer maxLength:sizeof(buffer)]; if (len > 0) { NSLog(@"Command send"); [oStream close]; } } break; case NSStreamEventErrorOccurred: event = @"NSStreamEventErrorOccurred"; statusText.text = @"Can not connect to the host!"; pingButton.hidden = YES; break; case NSStreamEventEndEncountered: event = @"NSStreamEventEndEncountered"; statusText.text = @"Connection closed by the server."; pingButton.hidden = YES; [theStream close]; [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [theStream release]; theStream = nil; break; default: event = @"** Unknown"; } NSLog(@"%@ : %@", io, event); }
(all I know!) Credits come deksa from this post (although I donโt know who the creator was because I saw it several times on the Internet, including here, SO). This code has been slightly modified by me (pingButton, statusText) if you want the original to go to the link mentioned.
The Apple Developer website contains information about this.
As I said, I saw some things similar to this on the Internet, but now I understand that everything that happens after the connection is โautomaticโ; for example, if the server is in standby mode using read() , case NSStreamEventHasSpaceAvailable: will be called automatically and all the code will be run there.
Now I believe that this question has answered.
source share