-1

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.

user7388
  • 1,741
  • 2
  • 19
  • 25
shizophren
  • 21
  • 7

1 Answers1

0

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.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thank you for you answer. If i implement the methods in my AppDelegate.m how can I call the method? I never worked with delegate methods. Greets – shizophren May 21 '13 at 12:53
  • @shizophren: It works the other way around: the GCDAsyncSocket library calls your delegate method. – Martin R May 21 '13 at 13:00
  • Could you be mor clearer? Where I should implement the method? And do I have to call it for myself, or will it done automatically by the GCDAsyncSocket library? I only want to know - success, or not. – shizophren May 21 '13 at 13:08
  • @shizophren: You have to implement the delegate methods. If you init the socket using `... initWithDelegate:self ... ` in your AppDelegate.m, then this AppDelegate is the delegate of the socket. So you implement the delegate methods in the same AppDelegate class. The GCDAsyncSocket library will call the delegate methods. `socket:didWriteDataWithTag:` is called if the write succeeded, and `socketDidDisconnect:withError` is called if the write failed. – Martin R May 21 '13 at 13:15
  • Ok, it becomes clearer to me :-) So i implemented the two methods. They should return an NSLog Massege for me. The socketDidDisconnect seems to work, the didWriteDataWithTag not. Both only have a NSLog statement in it. Any clue why? - (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag { NSLog(@"didWriteDataWithTag was successfull %lu", tag); } - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err { NSLog(@"ERROR to Connection %@", err); } – shizophren May 21 '13 at 13:22
  • @shizophren: I made a quick test by modifying the sample project "CocoaAsyncSocket-master/GCD/Xcode/ConnectTest/Mobile/ConnectTest" and everything seems to work for me. – Martin R May 21 '13 at 13:41