2011-04-06 203 views
2

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

感谢,

罗宾

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); 
} 

回答

6

header file

// 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:". 

如果检查the source - 为圣万物的爱,与开源软件工作时,使用源! - ,只有在启动连接过程失败时,才会看到您调用的方法返回NO。的YES返回值只是说,“好吧,我试图连接:

  • ”如果出了问题,我会让你知道通过调用onSocket:willDisconnectWithError:onSocketDidDisconnect:。 “
  • ”如果一切顺利,您会收到onSocket:didConnectToHost:port:消息,mmkay?“

不要期待异步套接字库的同步行为。

+0

需要点。如果它让你感觉更好,我确实看过了源头,但还不够清楚。谢谢 :) – Robin 2011-04-07 06:31:23

相关问题