我试图在同一个 GCDAsyncSocket 对象上“发送到”和“从”接收,但它Attempting to accept while connected or accepting connections. Disconnect first.
在尝试初始化对象时返回错误。
我的代码:
-(instancetype)initWithHost:(NSString *)host port:(NSInteger)port userData:(NSDictionary *)userData delegate:(id<AKSenderSocketDelegate>)delegate
{
self = [super init];
if (self)
{
self.delegate = delegate;
self.senderSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
self.senderSocket.userData = userData;
NSError *err = nil;
//---- Sender
if (![self.senderSocket connectToHost:host onPort:port error:&err])
{
NSLog(@"Failed to connect: %@", err);
}
else
{
NSLog(@"Connected to :%@:%ld",host,(long)port);
}
//---- Listener
if (![self.senderSocket acceptOnPort:0 error:&err])
{
NSLog(@"Failed to open lintening port. :%@",err.localizedDescription);
}
else
{
NSLog(@"Listening to port :%hu and host :%@",self.senderSocket.localPort,self.senderSocket.localHost);
}
}
return self;
}
请帮忙!!