【问题标题】:asyncsocket "connectToHost" always succeeds and never returns a failasyncsocket "connectToHost" 总是成功并且从不返回失败
【发布时间】:2011-04-06 22:18:29
【问题描述】:

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

谢谢,

罗宾

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

【问题讨论】:

    标签: objective-c sockets asyncsocket


    【解决方案1】:

    根据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 – 出于对所有神圣事物的热爱,在使用开源软件时,使用源代码! – 您将看到您正在调用的方法返回 @987654324 @仅当它无法启动连接过程时。 YES 的返回值只是说,“好的,我正在尝试连接:

    • “如果出现问题,我会致电onSocket:willDisconnectWithError:onSocketDidDisconnect: 通知您。
    • “如果一切顺利,你会收到onSocket:didConnectToHost:port: 消息,mmkay?”

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

    【讨论】:

    • 点了。如果它让你感觉更好,我确实看过源头,但不够彻底清楚。谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    相关资源
    最近更新 更多