[英]iOS reachability for socket connection
I need to check for reachability for a socket connection in my application. 我需要检查应用程序中套接字连接的可访问性。 I have gone through apple's sample code and few examples I could find from SO and other websites but I'm out of luck as I do not get any response.
我浏览了Apple的示例代码以及从SO和其他网站上可以找到的几个示例,但是我没有运气,因为我没有得到任何回应。 Here's a sample code I'm trying.
这是我正在尝试的示例代码。
My requirement is simply to verify connectivity to myhost:port which is a socket connection. 我的要求仅仅是验证与套接字连接的myhost:port的连接。 myhost is an address from dyndns.org
myhost是dyndns.org的地址
struct sockaddr_in server_address;
server_address.sin_len = sizeof(server_address);
server_address.sin_family = AF_INET;
server_address.sin_port = htons(1234);
server_address.sin_addr.s_addr = inet_addr("host-from-dyndns.org");
Reachability *reachability = [Reachability reachabilityWithAddress:&server_address];
__weak Reachability *weakReachability = reachability;
reachability.reachableBlock = ^(Reachability*reach)
{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"REACHABLE!");
});
[weakReachability stopNotifier];
};
reachability.unreachableBlock = ^(Reachability*reach)
{
NSLog(@"UNREACHABLE!");
[weakReachability stopNotifier];
};
[reachability startNotifier];
After few failed attempts of meddling with with reachability classes, I gave up and used a temporary asynchronous socket connection created on demand to verify the connection. 经过几次尝试与可达性类的失败尝试后,我放弃并使用了根据需要创建的临时异步套接字连接来验证连接。 It works as expected and is built on top of gcd.
它可以按预期运行,并建立在gcd之上。 Also I found the AsyncSocket by Robbie Hanson ( https://github.com/robbiehanson/CocoaAsyncSocket ) to be quite helpful while implementing for my requirement.
另外,我发现Robbie Hanson( https://github.com/robbiehanson/CocoaAsyncSocket )的AsyncSocket在实现我的要求时非常有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.