Question

I'm trying to test a server connection with the GCDAsyncSocket.

https://github.com/robbiehanson/CocoaAsyncSocket

I want to connect to a ip + port and get a message, whether it worked, or not.

I'm so far right now.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{ // Override point for customization after application launch.

dispatch_queue_t mainQueue = dispatch_get_main_queue();

asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:mainQueue];


    NSString *host = @"www.google.de";
    uint16_t port = 21;

    NSError *error = nil;

    if(![asyncSocket connectToHost:host onPort:port error:&error])
    {
        NSLog(@"ERROR %@!!!", error);
    }
    else{
        NSLog(@"NO ERROR %@!!!", error);
    }

    [asyncSocket writeData:@"DATA" withTimeout:3 tag:0];

return YES;

}

But how can i check whether the Data was written or not?

if(![asyncSocket connectToHost:host onPort:port error:&error])

always get me a no error.

Was it helpful?

Solution

You have to implement the socket:didWriteDataWithTag: delegate method. From "GCDAsyncSocket.h":

/**
 * Called when a socket has completed writing the requested data. Not called if there is an error.
**/
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag;

If the write times out then the connection is closed and the delegate method

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err;

is called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top