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);
}
根据头文件:
// 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:".
如果您查看源代码 - 为了对所有事物的热爱,在使用开源软件时,请使用源代码!- ,您将看到您调用的方法NO仅在无法启动连接过程时返回.返回值YES只是说,"好吧,我正在尝试连接:
onSocket:willDisconnectWithError:和电话通知你onSocketDidDisconnect:.onSocket:didConnectToHost:port:消息,mmkay?"不要指望来自异步套接字库的同步行为.
| 归档时间: | 
 | 
| 查看次数: | 1979 次 | 
| 最近记录: |