#import "TCP.h" @interface TCP () @end @implementation TCP //- (void)viewWillAppear:(BOOL)animated //{ // [super viewWillAppear:animated]; // // [self TcpClientInitialise]; //} static NSString * C_name; +(NSString *)getName { if(!C_name) { C_name=@"Heloo"; } return C_name; } +(void)setName:(NSString *)name { if(!C_name) { C_name=@"Helloo"; } C_name=name; } - (void)TcpClientInitialise { NSLog(@"Tcp Client Initialise"); CFReadStreamRef readStream; CFWriteStreamRef writeStream; CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.3.105", 9050, &readStream, &writeStream); InputStream = (__bridge NSInputStream *)readStream; OutputStream = (__bridge NSOutputStream *)writeStream; [InputStream setDelegate:self]; [OutputStream setDelegate:self]; [InputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [OutputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [InputStream open]; [OutputStream open]; } -(void)SendingData:(NSString *)sendingString{ NSLog(@"Ready to send data %@",sendingString); NSData *data = [[NSData alloc] initWithData:[sendingString dataUsingEncoding:NSASCIIStringEncoding]]; [OutputStream write:[data bytes] maxLength:[data length]]; //<<Returns actual number of bytes sent - check if trying to send a large number of bytes as they may well not have all gone in this write and will need sending once there is a hasspaceavailable event } - (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)StreamEvent { switch (StreamEvent) { case NSStreamEventOpenCompleted: NSLog(@"TCP Client - Stream opened"); break; case NSStreamEventHasBytesAvailable: if (theStream == InputStream) { uint8_t buffer[1024]; int len; while ([InputStream hasBytesAvailable]) { len = [InputStream read:buffer maxLength:sizeof(buffer)]; if (len > 0) { NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]; if (nil != output) { NSLog(@"TCP Client - Server sent: %@", output); //all programming for switching server reply NSArray *myArray = [output componentsSeparatedByString:@","]; } //Send some data (large block where the write may not actually send all we request it to send) int ActualOutputBytes = [OutputStream write:[OutputData bytes] maxLength:[OutputData length]]; //if (ActualOutputBytes >= ChunkToSendLength) //{ //It was all sent //[OutputData release]; //OutputData = nil; //} //else //{ //Only partially sent //[OutputData replaceBytesInRange:NSMakeRange(0, ActualOutputBytes) withBytes:NULL length:0]; //Remove sent bytes from the start //} } } } break; case NSStreamEventErrorOccurred: NSLog(@"TCP Client - Can't connect to the host"); break; case NSStreamEventEndEncountered: NSLog(@"TCP Client - End encountered"); [theStream close]; [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; break; case NSStreamEventNone: NSLog(@"TCP Client - None event"); break; case NSStreamEventHasSpaceAvailable: NSLog(@"TCP Client - Has space available event"); if (OutputData != nil) { //Send rest of the packet int ActualOutputBytes = [OutputStream write:[OutputData bytes] maxLength:[OutputData length]]; if (ActualOutputBytes >= [OutputData length]) { //It was all sent //[OutputData release]; OutputData = nil; } else { //Only partially sent [OutputData replaceBytesInRange:NSMakeRange(0, ActualOutputBytes) withBytes:NULL length:0]; //Remove sent bytes from the start } } break; default: NSLog(@"TCP Client - Unknown event"); } } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)