asyncsocket"connectToHost"总是成功,永远不会返回失败

Rob*_*bin 2 sockets objective-c asyncsocket

我正在使用asyncsocket创建一个与目标C的套接字连接.我使用"connectToHost"方法执行此操作.我试图处理套接字连接失败的情况.当连接成功时,"connectToHost"应该返回"YES",否则返回NO.出于某种原因,它总是返回是.我甚至提供了一个空字符串作为主机,它仍然返回yes.有什么想法吗?

谢谢,

知更鸟

BOOL connectStatus = NO; //used to check if connection attempt succeeded
testSocket = [[AsyncSocket alloc] initWithDelegate: self];
connectStatus = [testSocket connectToHost: @"" onPort: 5000 error: nil];

if(connectStatus == NO)
{
    NSLog(@"Failed to connect to socket ");

}

else {
    NSLog(@"Connected to socket sucessfully, connectStatus = %d", connectStatus);
}
Run Code Online (Sandbox Code Playgroud)

Jer*_*man 6

根据头文件:

// Once one of the accept or connect methods are called, the AsyncSocket instance is locked in
// and the other accept/connect methods can't be called without disconnecting the socket first.
// If the attempt fails or times out, these methods either return NO or
// call "onSocket:willDisconnectWithError:" and "onSockedDidDisconnect:".
Run Code Online (Sandbox Code Playgroud)

如果您查看源代码 - 为了对所有事物的热爱,在使用开源软件时,请使用源代码!- ,您将看到您调用的方法NO仅在无法启动连接过程时返回.返回值YES只是说,"好吧,我正在尝试连接:

  • "如果出现问题,我会通过电话onSocket:willDisconnectWithError:和电话通知你onSocketDidDisconnect:.
  • "如果一切顺利,你会得到一条onSocket:didConnectToHost:port:消息,mmkay?"

不要指望来自异步套接字库的同步行为.