Revision: 50029
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at August 4, 2011 18:13 by ghiboz
Initial Code
NSString *urlStr = @"192.168.178.26"; if (![urlStr isEqualToString:@""]) { NSURL *website = [NSURL URLWithString:urlStr]; if (!website) { NSLog(@"%@ is not a valid URL"); return; } NSHost *host = [NSHost hostWithName:[website host]]; [NSStream getStreamsToHost:host port:3258 inputStream:&iStream outputStream:&oStream]; [iStream retain]; [oStream retain]; [iStream setDelegate:self]; [oStream setDelegate:self]; [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [iStream open]; [oStream open]; } - (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent { NSString *io; if (theStream == iStream) io = @">>"; else io = @"<<"; NSString *event; switch (streamEvent) { case NSStreamEventNone: event = @"NSStreamEventNone"; resultText.font = [UIFont fontWithName:@"Helvetica" size:10.0]; resultText.textColor = [UIColor whiteColor]; resultText.text = [[NSString alloc] initWithFormat: @"Can not connect to the host!"]; break; case NSStreamEventOpenCompleted: event = @"NSStreamEventOpenCompleted"; break; case NSStreamEventHasBytesAvailable: event = @"NSStreamEventHasBytesAvailable"; if (theStream == iStream) { 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) { //NSArray *arr = [output componentsSeparatedByString:@"\n"]; resultText.font = [UIFont fontWithName:@"Helvetica" size:10.0]; resultText.textColor = [UIColor whiteColor]; resultText.text = [[NSString alloc] initWithFormat: @"%@\n%@", resultText.text, output]; NSLog(@"%@", output); [output release]; } } } } break; case NSStreamEventHasSpaceAvailable: event = @"NSStreamEventHasSpaceAvailable"; break; case NSStreamEventErrorOccurred: event = @"NSStreamEventErrorOccurred"; resultText.font = [UIFont fontWithName:@"Helvetica" size:10.0]; resultText.textColor = [UIColor whiteColor]; resultText.text = [[NSString alloc] initWithFormat: @"Can not connect to the host!"]; break; case NSStreamEventEndEncountered: event = @"NSStreamEventEndEncountered"; [theStream close]; [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [theStream release]; theStream = nil; break; default: event = @"** Unknown"; } NSLog(@"%@ : %@", io, event); }
Initial URL
http://www.iphonedevsdk.com/forum/iphone-sdk-development/2998-how-socket-connection.html
Initial Description
da testare
Initial Title
socket with iphone
Initial Tags
ios
Initial Language
Objective C