Question

Here's my objective-c code... I've tried the code that's commented out as well, but no luck. I simply want to get this to connect.

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];

    //get socket connecting...
    NSError *err = nil;

    uint16_t thePort = htons(1234);

//    struct sockaddr_in ip;
//    ip.sin_family = AF_INET;
//    ip.sin_port = htons(thePort);
//    inet_pton(AF_INET, "192.168.2.5", &ip.sin_addr);
//    
//    NSData* host = [NSData dataWithBytes:&ip length:ip.sin_len];
//    
//    if(![self.socket connectToAddress:host error:&err]) {
//        NSLog(@"Failed to connect... %@", err);
//    }


    if(![self.socket connectToHost:@"192.168.2.5" onPort:thePort error:&err]) {
        NSLog(@"Failed to connect %@", err);
    }
    // Do any additional setup after loading the view, typically from a nib.
}

When I run it this is all that I get...

2013-11-02 23:02:10.404 bonfire.alpha0.0[3892:70b] Failed to connect (null)

On the server side I'm using a simple implementation of an echo-server (can be found https://github.com/zappala/python-networking-and-threading/blob/master/echo-server/echoserver.py). I know physical connection between client and server (192.168.2.5) is good because when I run echoclient.py from the client computer it works just fine.

Was it helpful?

Solution

Okay I found the error, very noobish one haha. I wasn't initiating the GCDAsyncSocket properly. Here's the proper code for initiating it.

self.socket = [[GCDAsyncSocket alloc] initWithDelegate:self.sDelegate delegateQueue:dispatch_get_main_queue()];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top