I am trying to use the GCDAsyncSocket
class to connect to a device but am having trouble. It seems to fail to inform me if it connects, but also fails to inform me it failed to connect.
Here is my .h file
#import <Foundation/Foundation.h>
#import "GCDAsyncSocket.h"
@interface SocketTestClass : NSObject <GCDAsyncSocketDelegate>
{
GCDAsyncSocket* socket;
}
@end
and here is my .m file
@implementation SocketTestClass
- (void)dealloc
{
[self disconnect];
}
- (void)connect
{
NSLog(@"Ready to create socket");
socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError* error = Nil;
if (![socket connectToHost:@"192.168.1.200" onPort:80 withTimeout:10 error:&error])
{
NSLog(@"Failed to create socket");
}
}
- (void)disconnect
{
if ([socket isConnected])
{
[socket disconnect];
}
}
- (void)socket:(GCDAsyncSocket *)sock
didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
NSLog(@"Socket did accept new socket");
}
- (void)socket:(GCDAsyncSocket *)sock
didConnectToHost:(NSString *)host
port:(uint16_t)port
{
NSLog(@"Socket did connect to host on port");
}
- (void)socket:(GCDAsyncSocket *)sock
didReadData:(NSData *)data
withTag:(long)tag
{
NSLog(@"Socket did read data with tag");
}
- (void)socket:(GCDAsyncSocket *)sock
didReadPartialDataOfLength:(NSUInteger)partialLength
tag:(long)tag
{
NSLog(@"Socket did read partial data of length");
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
NSLog(@"Socket did write data with tag");
}
- (void)socket:(GCDAsyncSocket *)sock
didWritePartialDataOfLength:(NSUInteger)partialLength
tag:(long)tag
{
NSLog(@"Socket did write partial data of length");
}
- (void)socketDidCloseReadStream:(GCDAsyncSocket *)sock
{
NSLog(@"Socket did close read stream");
}
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock
withError:(NSError *)err
{
NSLog(@"Socket did disconnect with error");
}
- (void)socketDidSecure:(GCDAsyncSocket *)sock
{
NSLog(@"Socket did secure");
}
@end
From my view controller I call the connect method of this class but the only thing that prints out to the console is:
Ready to create socket
Note: I have implemented all delegates for GCDAsyncSocket
and have all writing to the console via NSLog
.
Any idea as to why I do not get the message "Socket did connect to host on port" or the message "Socket did disconnect with error"?