我正在尝试使用GCDAsyncSocket该类连接到设备,但遇到了问题。它似乎没有通知我它是否连接,但也没有通知我它未能连接。
这是我的 .h 文件
#import <Foundation/Foundation.h>
#import "GCDAsyncSocket.h"
@interface SocketTestClass : NSObject <GCDAsyncSocketDelegate>
{
    GCDAsyncSocket* socket;
}
@end
这是我的 .m 文件
@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
从我的视图控制器中,我调用了这个类的 connect 方法,但唯一打印到控制台的是:
Ready to create socket
注意:我已经为控制台实现了所有委托,GCDAsyncSocket并通过NSLog.
关于为什么我没有收到消息“Socket did connect to host on port”或消息“Socket did disconnect with error”的任何想法?