gpt4 book ai didi

iphone - 地址可达性 - 服务器和端口 - iOS 5

转载 作者:太空狗 更新时间:2023-10-30 04:02:17 25 4
gpt4 key购买 nike

我正在尝试检查服务器是在线还是离线:我遇到的问题是它在连接时有一个端口

我目前的代码:

struct sockaddr_in address;
address.sin_len = sizeof(address);
address.sin_family = AF_INET;
address.sin_port = htons(25667);
address.sin_addr.s_addr = inet_addr("fr7.mooshroom.net");

Reachability *reachability = [Reachability reachabilityWithAddress:&address];

请让我知道我做错了什么。并且请不要将我链接到其他问题,我已经搜索过但没有一个有我要找的东西。

最佳答案

inet_addr() 函数基本上不会为您做域名解析。您需要向其传递一个 IP 地址(例如 127.0.0.1)。

要将 DNS 名称解析为 IP 地址,您需要查看标准的 gethostbyname() 函数。

澄清一下:

struct hostent *host = gethostbyname("fr7.mooshroom.net");
if (host) {
struct in_addr in;
NSLog(@"HOST: %s" , host->h_name);
while (*host->h_addr_list)
{
bcopy(*host->h_addr_list++, (char *) &in, sizeof(in));
NSLog(@"IP: %s", inet_ntoa(in));
}
}

现在,说了这么多,您确定这会如您所愿吗? SCNetworkReachabilityRef 的文档建议不要:

http://developer.apple.com/library/ios/#documentation/SystemConfiguration/Reference/SCNetworkReachabilityRef/Reference/reference.html

"A remote host is considered reachable when a data packet, sent by an application into the network stack, can leave the local device. Reachability does not guarantee that the data packet will actually be received by the host."

关于iphone - 地址可达性 - 服务器和端口 - iOS 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11395397/

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