Unable to Send asynchronous request in iOS Using Objective C language

375 views
Skip to first unread message

Ashwinkumar Damji

unread,
Sep 7, 2016, 4:26:12 AM9/7/16
to CocoaAsyncSocket
I am using GCDAsyncSocket library for Socket communication.

Socket Server is implemented in .NET and Socket client is implemented in iOS using Objective C.

When I send one request, after reply socket server closes the connection for security reason.

When I am sending 4 request simultaneously after first request's response the socket gets closed due to that the remaining 2nd, 3rd, and 4th request does not process.

Currently it's not working Asynchronously. Can anyone please provide me any help to achieve asynchronous communication with Socket.

Please check my below code.


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    [self.asyncSocket setDelegate:self];
    
    NSString *requestString = @"MESSAGE_TO_SEND";
    
    NSError *error = nil;
    
    if (![self.asyncSocket connectToHost:HOST_IP onPort:PORT error:&error]) {
        NSLog(@"Error in connectToHost: %@", error);
    }
   
    NSData *requestData = [requestString dataUsingEncoding:NSUTF8StringEncoding];
    [self.asyncSocket writeData:requestData withTimeout:-1 tag:0];
    
    NSData *requestData2 = [requestString dataUsingEncoding:NSUTF8StringEncoding];
    [self.asyncSocket writeData:requestData2 withTimeout:-1 tag:1];
}


AsyncSocket Delegate methods


#pragma mark - GCDAsyncSocket Delegate
-(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port {
    NSLog(@"%s host: %@, port: %u", __PRETTY_FUNCTION__, host, port);
    [self.asyncSocket readDataWithTimeout:-1 tag:0];
}
-(void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{
    NSLog(@"%s, err: %@", __PRETTY_FUNCTION__, err);
    
}
-(void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag {
    NSLog(@"%s request send for %li", __PRETTY_FUNCTION__, tag);
    //[sock readDataToData:[GCDAsyncSocket LFData] withTimeout:30 tag:tag];
    [sock readDataWithTimeout:30 tag:0];
}
-(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
    [sock readDataWithTimeout:-1 tag:0];
    NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSLog(@"%s request send for %li, dataString = %@", __PRETTY_FUNCTION__, tag, dataString);
    
}
- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
    NSLog(@"Accepted new socket from %@:%hu", [newSocket connectedHost], [newSocket connectedPort]);
    
    self.asyncSocket = newSocket;
    NSString *welcomMessage = @"Hello from the server\r\n";
    [self.asyncSocket writeData:[welcomMessage dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:1];
    
    [self.asyncSocket readDataWithTimeout:-1 tag:0];
    
}
-(void)socket:(GCDAsyncSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength tag:(long)tag {
    NSLog(@"%s request send for %li", __PRETTY_FUNCTION__, tag);
}
 

I am getting below error after sending first request.

-[ViewController socketDidDisconnect:withError:], err: Error Domain=GCDAsyncSocketErrorDomain Code=8 "Error creating CFStreams" UserInfo=0x16655060 {NSLocalizedDescription=Error creating CFStreams}

due to above error second request does not sent to server.
 

Thanks in advance.




Arno Gramatke

unread,
Sep 7, 2016, 9:49:44 AM9/7/16
to CocoaAsyncSocket
Are you sure that the connection is setup correctly and that even the first request is being transmitted? connectToHost:onPort:err usually returns immediately and starts to initiate the connection in the background. It will notify you with a call to didConnectToHost. If that didn't work and you only see the socketDidDisconnect call.

Apart from that if your socket is only a client socket you don't need socket:didAcceptNewSocket:. It is only used when you create a server.

Another thought: What should happen when the server closes the connection after the first request was sent? If the socket is being closed, you have to reconnect to be able to send another request...
Reply all
Reply to author
Forward
0 new messages