gpt4 book ai didi

objective-c - asyncsocket "connectToHost"总是成功并且永远不会返回失败

转载 作者:行者123 更新时间:2023-12-02 08:55:02 25 4
gpt4 key购买 nike

我正在使用 asyncsocket 创建与 Objective C 的套接字连接。我使用“connectToHost”方法来执行此操作。我正在尝试处理套接字连接失败的情况。当连接成功时,“connectToHost”应该返回“YES”,否则返回“NO”。由于某种原因,它总是返回"is"。我什至提供了一个空白字符串作为主机,它仍然返回 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);
}

最佳答案

根据 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: 消息,嗯?”

不要期望异步套接字库具有同步行为。

关于objective-c - asyncsocket "connectToHost"总是成功并且永远不会返回失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5573664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com