I want to establish socket connection to streaming server (with iphone ) and want to download its content like image,.css,etc to iphone. Any Idea or sample code is can help me. I need to write code for client only.
I want to establish socket connection to streaming server (with iphone ) and want
Share
Establish Connection as follows and change the urlStr to your server URL
NSString *urlStr = @"http://192.168.0.108"; NSURL *website = [NSURL URLWithString:urlStr]; CFReadStreamRef readStream; CFWriteStreamRef writeStream; CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[website host], 1234, &readStream, &writeStream); NSInputStream *inputStream = (NSInputStream *)readStream; NSOutputStream *outputStream = (NSOutputStream *)writeStream; [inputStream setDelegate:self]; [outputStream setDelegate:self]; [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [inputStream open]; [outputStream open];Make Use of NSStream Delegate as follows to read data
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode { switch(eventCode) { case NSStreamEventHasBytesAvailable: { NSLog(@"Bytes Available"); uint8_t b[1024]; unsigned int len = 0; NSMutableData *data = [[NSMutableData alloc] init]; len = [(NSInputStream *)stream read:b maxLength:1024]; if(!len) { if ([stream streamStatus] != NSStreamStatusAtEnd) { } } else { [data appendBytes:(const void *)b length:len]; int bytesRead; bytesRead += len; //make use of data here } } break; } }