How do I know if an iOS application for my client is connected to the server?

I press the button to connect to the server (TCP), but I do not know if it is connected or not. Here is this piece of code:

[self connectToServerUsingCFStream:msg portNo:50000];

  if(readStream && writeStream) { NSString *newText = [[NSString alloc] initWithFormat:@"Connected!! :)"]; statusText.text = newText; [newText release]; pingButton.hidden = NO; } else { NSString *newText = [[NSString alloc] initWithFormat:@"Connection unsuccessful :("]; statusText.text = newText; [newText release]; } 

I always get "Connected !! :)" even if the server is down: s

+1
source share
2 answers

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.

+7
source

Although you did not provide enough information, I would suggest using ASIHTTPRequest for HTTP and AsyncSocket for TCP and UDP. If the connection has been established, callback methods will be started,

I have to say that my experience with CFNetwork is very limited, but it seems to me that you are just testing if stream objects exist ( if(readStream && writeStream) ).
A quick look at CFNetwork Programming Guide: Working with Read Streams tells me that you should open it with CFReadStreamOpen() , this function will return a boolean value if it really opened the stream.

 if (!CFReadStreamOpen(myReadStream)) { CFStreamError myErr = CFReadStreamGetError(myReadStream); // An error has occurred. if (myErr.domain == kCFStreamErrorDomainPOSIX) { // Interpret myErr.error as a UNIX errno. } else if (myErr.domain == kCFStreamErrorDomainMacOSStatus) { // Interpret myErr.error as a MacOS error code. OSStatus macError = (OSStatus)myErr.error; // Check other error domains. } } 

BTW:
instead

 NSString *newText = [[NSString alloc] initWithFormat:@"Connected!! :)"]; statusText.text = newText; [newText release]; 

You can simply write statusText.text = @"Connected!! :)";

+1
source

All Articles