I'm trying to set up a TCP connection to a local server, and since pretty much everyone agrees that Asyncsocket is the way to go, I went for it, too. However, I'm running into problems at the most basic level: the Asyncsocket instance doesn't have a local or remote address. I don't have much code, but this is what I do have:
- (void)viewDidLoad {
[super viewDidLoad];
AsyncSocket *socket = [[AsyncSocket alloc] initWithDelegate:self];
[socket connectToHost:@"www.google.com" onPort:80 error:nil];
}
- (BOOL)onSocketWillConnect:(AsyncSocket *)sock {
NSLog(@"%@", sock);
return YES;
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port {
NSLog(@"%@", host);
}
The following shows up in the console (from the NSLog in onSocketWillConnect):
AsyncSocket 0x298de0 local nowhere remote nowhere has queued 0 reads 0 writes, no current read, no current write, read stream 0x299720 not open, write stream 0x299aa0 not open, not connected
Where it says "nowhere", of course it should have IP addresses, but it doesn't. From examples online it looks like my code is fine, yet I get this "nowhere" thing when running on two computers and my phone, so I'm probably doing something wrong. Anybody have an idea?
Thanks!
edit: For clarification: if I use error reporting (by passing an NSError instance to "connectToHost:onPort:error" and NSLogging that), all I get back is (null)
.
Wow, that's embarrassing. I needed to make an ivar to store the AsyncSocket, because without it, it went out of scope after viewDidLoad completed, which was why my onSocket:didConnectToHost:port wasn't called.