gpt4 book ai didi

iphone - 在 AsyncSocket 类中调用 connecttohost

转载 作者:行者123 更新时间:2023-12-03 19:00:09 25 4
gpt4 key购买 nike

我已经在 iPad 上测试了此代码:

// execute the function when press button
- (IBAction)connect:(id)sender {
//[super viewDidLoad];

AsyncSocket *socket1=[[AsyncSocket alloc] initWithDelegate:self];
BOOL pass = [socket1 connectToHost:@"www.126.com" onPort:80 error:nil]; // but when I break the wifi,connecttohost still return yes
if(pass)
{
[connectbtn setTitle:@"connected" forState:UIControlStateNormal];
}

[socket1 readDataWithTimeout:3 tag:1];
[socket1 writeData:[@"GET / HTTP/1.1\n\n" dataUsingEncoding:NSUTF8StringEncoding] withTimeout:3 tag:1];

}

请参阅上面代码中的注释。

当我断开 Wifi connecttohost 时,返回 yes。我不知道问题出在哪里。有人可以帮助我并给我一些指示吗?

最佳答案

出于某种原因,在这种情况下 connectToHost 将始终返回 true。您需要依赖委托(delegate)方法。

- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port {
NSLog(@"Connected To %@:%i.", host, port);
}

- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err {
NSLog(@"Disconnecting. Error: %@", [err localizedDescription]);
}

- (void)onSocketDidDisconnect:(AsyncSocket *)sock {
NSLog(@"Disconnected.");

[socket setDelegate:nil];
[socket release];
socket = nil;
}

- (BOOL)onSocketWillConnect:(AsyncSocket *)sock {
NSLog(@"onSocketWillConnect:");
return YES;
}

关于iphone - 在 AsyncSocket 类中调用 connecttohost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13601099/

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