Socket is disconnected immediately after making connection by using GCDAsyncSocket library in iPhone application

2.5k views Asked by At

I want to read and write data from OBD2 Wifi enabled Adapter which is used to read car data and fault codes. For that ,I'm using GGCDAsyncSocket Library for socket connection.

You can download CocoaAsyncSocket Library source code

But it maintains connection hardly for 10 seconds and sometimes 40-50 seconds and then terminated.

And the logs shows :

Socket disconnected : Error Domain=NSPOSIXErrorDomain Code=57 "The operation couldn’t be completed. Socket is not connected" UserInfo=0x14cd50 {}

Any help or idea would be appreciated !

Thanks in advance !

Please look at below code :

- (void)connectObdKeyHost:(NSString*)strHost port:(NSString *)strPort {
        [gcdAsyncSocket connectToHost:strHost onPort:portNo withTimeout:30 error:&error];
}

// GCDAsyncSocket Delegate Methods
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port {
   NSLog(@"Socket connected : ");
}

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

There are 1 answers

1
Nirmal Shah On

I tried with below method :

    [gcdAsyncSocket connectToHost:strHost onPort:portNo error:&error];

and now it's working.