Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CocoaAsyncSocket's read and write delegates are not firing & code organization

I'm trying to do the following with a modified version of the echo server example that comes with the cocoaasyncsocket library:

1) open a connection to a python script acting as a server
2) send data // works, but delegate doesn't fire
3) receive data back // delegate doesn't fire
4) disconnect // doesn't disconnect, apparently still in my thread

Currently I open a connection in the didFinishLaunchingWithOptions delegate, and then attempt to send data in the didConnectToHost delegate. I then attempt to read data coming back from the client and then disconnect.

I am able to open a connection and send data (which the server verifies as received) but the didWriteDataWithTag delegate never fires. However, the server receive the data. The server then fires back some data, but the didReadData doesn't fire either.

Beside the fact the read/write delegates aren't firing, it seems the way I'm organizing my code is not right, but I'm not sure how this looks in an event-driven system as opposed to run loop (I'm a novice at event-driven stuff + networking). If I have a series of actions whose respective completions are triggered by their delegates, should the delegates be sharing some sort of messages- ie we recieved an "xxx" message, write back "yyy"? I'd prefer to have one function which manages all of this. Is there a canonical way of doing this?

IPhoneConnectTestAppDelegate.m (snippets)

- (void)localConnect {
    NSError *error = nil;
    if (![asyncSocket connectToHost:@"localhost" onPort:5000 error:&error]) {
        DDLogError(@"Error connecting: %@", error);
    }
}

- (void)disconnect {
    [asyncSocket setDelegate:nil];
    [asyncSocket disconnect];
    [asyncSocket release];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Setup our socket (GCDAsyncSocket).
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:mainQueue];

    [self localConnect];

    // Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}


- (void)onSocket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
    NSString *output = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    NSLog(@"didReadData: %@", output);

}

- (void)onSocket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag {
    NSLog(@"didWriteDataWithTag");
}

- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port {
    NSLog(@"socket:%p didConnectToHost:%@ port:%hu", sock, host, port);
    if(port == 5000)
    {
        NSString *msg = @"q";
        NSData *dataOut = [msg dataUsingEncoding:NSASCIIStringEncoding];
        [asyncSocket writeData:dataOut withTimeout:-1 tag:0];
        [asyncSocket readDataWithTimeout:-1 tag:0];
        [self disconnect];
    }
}

tcpserver.py

# TCP server example
import socket, time
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 5000))
server_socket.listen(5)

print "TCPServer Waiting for client on port 5000"

while 1:
    client_socket, address = server_socket.accept()
    print "I got a connection from ", address
    while 1:
        data = client_socket.recv(512)
        print "Data from client",data

        time.sleep(2)
        data = "xxx"
        print "Sending data to client",data
        client_socket.send (data)
        break;
like image 867
nflacco Avatar asked Dec 17 '22 12:12

nflacco


1 Answers

I know this is an old question with an already accepted answer, but to clarify for people who find this thread looking for something, the reason the delegate methods didn't get called is because the GCDAsynchSocket start with socket: instead of onsocket: ie:

- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag

becomes:

- (void) socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
like image 158
Mike Wallace Avatar answered Dec 19 '22 02:12

Mike Wallace