iPhone SDK 可达性WithAddress 问题 [英] iPhone SDK reachabilityWithAddress issue

查看:14
本文介绍了iPhone SDK 可达性WithAddress 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要检查与 IP 地址的连接.我正在尝试使用reachabilityWithAddress 选项使用Apple Reachability 类来做到这一点.我的问题是我可以将任何 IP 地址放在 callAddress.sin_addr.s_addr 字段中,并且 statusText 总是可达".如何准确检查与 IP 地址的连接?

I need to check connectivity with IP address only. I'm trying to do it with Apple Reachability class, using reachabilityWithAddress option. And my problem is I can put any IP address in callAddress.sin_addr.s_addr field and statusText always will be "reachble". How can I do to exactly check connectivity to IP address?

-(BOOL)reachabilityTest {

 struct sockaddr_in callAddress;
 callAddress.sin_len = sizeof(callAddress);
 callAddress.sin_family = AF_INET;
 callAddress.sin_port = htons(24);
 callAddress.sin_addr.s_addr = inet_addr("212.83.3.190");
 Reachability *hostReach = [[Reachability reachabilityWithAddress:&callAddress] retain];

 NetworkStatus netStatus = [hostReach currentReachabilityStatus];

 if (netStatus == NotReachable)
 {
  NSLog(@"NotReachable");
  statusField.text = @"NOT reachable";
  return NO;  
 }

 if (netStatus == ReachableViaWiFi)
 {
  NSLog(@"ReachableViaWiFi");
  statusField.text = @"reachable";
  return YES;
 } 
 [Reachability release];
 }

推荐答案

不要使用 Reachability 类,而是使用老式的方法并打开到它的 TCP 连接.如果对 connect() 的响应是 EHOSTUNREACH,那么你不能.如果响应是ECONNREFUSED,或者连接成功,那么就可以了.

Instead of using the Reachability classes, go old-school and open a TCP connection to it. If the response to connect() is EHOSTUNREACH, then you can't. If the response is ECONNREFUSED, or a successful connection, then you can.

这篇关于iPhone SDK 可达性WithAddress 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆