iphone – 在AsyncSocket类中调用connecttohost

iphone – 在AsyncSocket类中调用connecttohost,第1张

概述我在iPad上测试了这段代码: // execute the function when press button- (IBAction)connect:(id)sender { //[super viewDidLoad]; AsyncSocket *socket1=[[AsyncSocket alloc] initWithDelegate:self]; BOOL pa 我在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返回是.我不知道问题出在哪里.谁能帮助我并给我一些指示?

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

- (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全部内容,希望文章能够帮你解决iphone – 在AsyncSocket类中调用connecttohost所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1082082.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存