Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple Iphone Client Connection to Server
    text
    copied!<p>Thanks in advance those who view my question. i am new to programming and also new to Objective-c programming, i am creating a simple client that will connect to a daytime server and request what time and date it is then print it to the screen, i have watched and read lots of tutorials right now and came up with the below code but i have a problem i cant read input from user that will enter the server address and i will use that server address in order to connect to server here is my code</p> <p>my socket codes in my controller.m file</p> <pre><code>@interface NSStream (MyAdditions) + (void)getStreamsToHostNamed:(NSString *)hostName port:(NSInteger)port inputStream:(NSInputStream **)inputStreamPtr outputStream:(NSOutputStream **)outputStreamPtr; @end @implementation NSStream (MyAdditions) + (void)getStreamsToHostNamed:(NSString *)hostName port:(NSInteger)port inputStream:(NSInputStream **)inputStreamPtr outputStream:(NSOutputStream **)outputStreamPtr { CFReadStreamRef readStream; CFWriteStreamRef writeStream; assert(hostName != nil); assert( (port &gt; 0) &amp;&amp; (port &lt; 65536) ); assert( (inputStreamPtr != NULL) || (outputStreamPtr != NULL) ); readStream = NULL; writeStream = NULL; CFStreamCreatePairWithSocketToHost( NULL, (CFStringRef) hostName, port, ((inputStreamPtr != nil) ? &amp;readStream : NULL), ((outputStreamPtr != nil) ? &amp;writeStream : NULL) ); if (inputStreamPtr != NULL) { *inputStreamPtr = [NSMakeCollectable(readStream) autorelease]; } if (outputStreamPtr != NULL) { *outputStreamPtr = [NSMakeCollectable(writeStream) autorelease]; } } @end </code></pre> <p>My Connection address and port</p> <pre><code>@implementation iPhoneClientViewController -(void) connect { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *address = [defaults stringForKey:@"Address"]; // this is default one if(!address) address = @"localhost"; NSLog(@"ADDRESS %@",address); [NSStream getStreamsToHostNamed: address port:13 inputStream:&amp;iStream outputStream:&amp;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]; } </code></pre> <p>my defaults are </p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt; &lt;plist version="1.0"&gt; &lt;dict&gt; &lt;key&gt;Title&lt;/key&gt; &lt;string&gt;iPhoneClient&lt;/string&gt; &lt;key&gt;StringsTable&lt;/key&gt; &lt;string&gt;Root&lt;/string&gt; &lt;key&gt;PreferenceSpecifiers&lt;/key&gt; &lt;array&gt; &lt;dict&gt; &lt;key&gt;Type&lt;/key&gt; &lt;string&gt;PSTextFieldSpecifier&lt;/string&gt; &lt;key&gt;Title&lt;/key&gt; &lt;string&gt;Server IP&lt;/string&gt; &lt;key&gt;Key&lt;/key&gt; &lt;string&gt;Address&lt;/string&gt; &lt;key&gt;DefaultValue&lt;/key&gt; &lt;string&gt;localhost&lt;/string&gt; &lt;key&gt;IsSecure&lt;/key&gt; &lt;false/&gt; &lt;key&gt;KeyboardType&lt;/key&gt; &lt;string&gt;NumbersAndPunctuation&lt;/string&gt; &lt;key&gt;AutoCorrectType&lt;/key&gt; &lt;string&gt;No&lt;/string&gt; &lt;/dict&gt; &lt;/array&gt; &lt;/dict&gt; &lt;/plist&gt; </code></pre> <p>i put a text field to my ViewController.m</p> <pre><code>-(BOOL)textFieldShouldReturn:(UITextField *)textField{ NSLog(@"Text Field contents %@",textField.text); [textField resignFirstResponder]; return YES; } </code></pre> <p>and when button clicked i put </p> <pre><code>- (IBAction)connectCommand:(id)sender { // NSString *address = textField.text; // this is gives me error textField undeclared // then i try this one NSString *address = text.text; if( iStream != nil) return; [self connect]; } </code></pre> <p>then i run program i enter the address then i click to connect button but it doesnt work it gives me following errors on output screen</p> <pre><code>2011-10-03 17:49:14.903 iPhoneClient[360:b303] Text Field contents 64.90.182.55 2011-10-03 17:49:16.096 iPhoneClient[360:b303] ADDRESS localhost 2011-10-03 17:49:16.101 iPhoneClient[360:b303] &gt;&gt; : NSStreamEventErrorOccurred 2011-10-03 17:49:16.102 iPhoneClient[360:b303] &lt;&lt; : NSStreamEventErrorOccurred </code></pre> <p>my event handling is like following </p> <pre><code>- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent { NSString *io; if (theStream == iStream) io = @"&gt;&gt;"; else io = @"&lt;&lt;"; NSString *event; switch (streamEvent) { case NSStreamEventNone: event = @"NSStreamEventNone - Can not connect to the host!"; break; case NSStreamEventOpenCompleted: event = @"NSStreamEventOpenCompleted"; 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 &gt; 0) { NSString *input = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]; if (nil != input) { //do something with data NSLog(@"%@",input); } } } } break; case NSStreamEventHasSpaceAvailable: event = @"NSStreamEventHasSpaceAvailable"; break; case NSStreamEventErrorOccurred: event = @"NSStreamEventErrorOccurred"; break; case NSStreamEventEndEncountered: event = @"NSStreamEventEndEncountered"; [self disconnect]; break; default: event = @"** Unknown"; } NSLog(@"%@ : %@", io, event); } </code></pre> <p>can any one help me with my code? or suggests me a good me a good tutorial that i can see examples of codes and learn more stuff about sockets.</p> <p>=====================================================================================================</p> <p>alright since i am new it is not allowed to reply my own post so i will edit it </p> <p>i think i will go with low level, yes dealing with xml is little bit complicating so i just disabled the parts </p> <pre><code>//NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; //NSString *address = [defaults stringForKey:@"Address"]; //if(!address) address = @"localhost";] </code></pre> <p>and put this code </p> <pre><code> NSString *address = [text text]; NSLog(@"ADDRESS %@",address); </code></pre> <p>now in my output screen i get following = </p> <pre><code>2011-10-03 18:58:57.758 iPhoneClient[424:b303] Text Field contents 64.90.182.55 2011-10-03 18:58:58.627 iPhoneClient[424:b303] ADDRESS 64.90.182.55 2011-10-03 18:58:58.685 iPhoneClient[424:b303] &gt;&gt; : NSStreamEventOpenCompleted 2011-10-03 18:58:58.686 iPhoneClient[424:b303] &lt;&lt; : NSStreamEventOpenCompleted 2011-10-03 18:58:58.686 iPhoneClient[424:b303] &lt;&lt; : NSStreamEventHasSpaceAvailable 2011-10-03 18:58:58.696 iPhoneClient[424:b303] &gt;&gt; : NSStreamEventHasBytesAvailable 2011-10-03 18:58:58.698 iPhoneClient[424:b303] &gt;&gt; : NSStreamEventEndEncountered </code></pre> <p>i thinks it says it connected but since i do nothing with data it is terminating itself because i put a run loop above?</p> <p>now anyone know how can i fetch and print date and time from server? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload