2

I've made a client with a class named "NetClass" using GCDAsyncSocket and importing CGDAsyncSocket.h. Than in my LoginViewController I've called my net class function to connect to the server.

On the server side I see that client is connected, but on the client side didConnectToHost doesn't get called.

My NetClass call in LoginViewController:

NetClass *nc = [[NetClass alloc] init];
    [nc ReceiveData:ip login:login password:md5 ];

And my NetClass functions:

- (BOOL)ReceiveData:(NSString *)ip login:(NSString*)login password:(NSString*)password
{
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    asyncSocket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:mainQueue];

    NSError *error = nil;
    uint16_t port = 2012;
    if (![asyncSocket connectToHost:ip onPort:port error:&error])
    {
        NSLog(@"Error connecting: %@", error);
        return NO;
    }
    else
    {
        NSLog(@"%@",asyncSocket.connectedHost);
        NSData *data = [[NSString stringWithFormat:@"<MOBIL><refreshall>TRUE</refreshall><user>%@</user><password>%@</password>",@"tst",@"tsts"] dataUsingEncoding:NSASCIIStringEncoding];
        NSData *enddata = [[NSString stringWithFormat:@"</ddodata"] dataUsingEncoding:NSASCIIStringEncoding];
        [asyncSocket writeData:data withTimeout:-1 tag:1];
        [asyncSocket readDataToData:enddata  withTimeout:-1 maxLength:-1 tag:1];
        [asyncSocket disconnect];
        return YES;
    }


}
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
    NSLog(@"%@ %@",@"CONNECTED TO HOST",host);
}

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
{
    NSLog(@"socketDidDiscoffffnnect:%p withError: %@", sock, err);
}

But at the same time function socketDidDisconnect is getting called

NSLog output :

  2012-08-25 14:16:46.312 TacticalView[34981:f803] (null)
2012-08-25 14:16:46.313 TacticalView[34981:f803] socketDidDiscoffffnnect:0x88024d0 withError: (null)

I see that asyncSocket not connecting when change ReceiveData like this:

 if (![asyncSocket connectToHost:ip onPort:port error:&error])
    {
        NSLog(@"Error connecting: %@", error);
        return NO;
    }
    else
    {
        if ([asyncSocket isConnected])
        {

        NSData *data = [[NSString stringWithFormat:@"<MOBIL><refreshall>TRUE</refreshall><user>%@</user><password>%@</password>",@"tst",@"tsts"] dataUsingEncoding:NSASCIIStringEncoding];

        NSData *enddata = [[NSString stringWithFormat:@"</ddodata"] dataUsingEncoding:NSASCIIStringEncoding];

        [asyncSocket writeData:data withTimeout:-1 tag:1];
        [asyncSocket readDataToData:enddata  withTimeout:-1 maxLength:-1 tag:1];
        [asyncSocket disconnect];
        }
        return YES;
    }

And if i make socket in my LoginViewController the didConnectToHost method is calling perfectly/ What's wrong& Why i can't use this method in my NetClass?

  • the same problem is here - http://stackoverflow.com/questions/10347066/gcdasyncsocket-no-connection-ios5?s=3d272901-fc83-4870-a97a-c1fc7e78147d but there id no answer – user1624340 Aug 25 '12 at 14:17

1 Answers1

0

It's a known fact that GCDAsyncSocket does not call the didConnectToHost: method. I suggest using AsyncSocket instead or calling the delegate method yourself.

  • What do you mean speaking about calling the delegate method yourself? By the way GCDAsyncSocket isn't calling DidReadData method.. how i can receive some data from server – user1624340 Aug 25 '12 at 12:43
  • And if i make socket in my LoginViewController the didConnectToHost method is calling perfectly/ What's wrong& Why i can't use this method in my NetClass? – user1624340 Aug 25 '12 at 12:54