iphone - iOS ping 和 ttl 超时

标签 iphone ios networking timeout ping

我想使用超时和 TTL 进行 ping。我使用 Apple 的代码 ( "Simple Ping" )。我读了 "iOS ping with timeout" .我更改代码:

CFSocketNativeHandle sock = CFSocketGetNative(self->_socket);
struct timeval tv;
tv.tv_sec  = 0;
tv.tv_usec = 100000; // 0.1 sec
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (void *)&tv, sizeof(tv));

bytesSent = sendto(
    sock,
    [packet bytes],
    [packet length], 
    0, 
    (struct sockaddr *) [self.hostAddress bytes], 
    (socklen_t) [self.hostAddress length]
);

但我不明白应该将显示接收数据包超时的代码放在哪里。我还需要使用 TTL(生存时间)信息进行 ping。我想根据这种模式获取信息:icmp_seq=count from=ip_address ttl=value_of_ttl time=value_of_replytime_ms

最佳答案

要修改 IP header 中的默认 TTL,请使用 IP_TTL 作为参数调用 setsockopt(已使用 IPv4 测试):

- (BOOL)setTTL:(int)ttl{
    CFSocketNativeHandle sock = CFSocketGetNative(self->_socket);
    int status  = setsockopt(sock, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
    if(status < 0)
    {
        return NO;
    }
    return YES;
}

"iOS ping with timeout"示例向套接字添加输出超时。据我了解,如果在此期间套接字未发送数据包,它将超时。我可能是错的,但我无法从 ICMP header 和 IPv4 header ( ICMP Packet format ) 中找到此“超时”值。 下面是使用苹果的简单 ping 捕获的控制台日志和请求、响应数据包:

enter image description here

enter image description here

enter image description here

如果您只想知道 ping 的响应时间,我想您可以自己在委托(delegate)方法中跟踪它。获取“didSendPacket”和“didReceivePingResponsePacket”函数调用时的时间戳,然后比较差异。

关于iphone - iOS ping 和 ttl 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14252064/

相关文章:

ios - 奇怪的 iOS8 + Unity3D 打嗝

具有 DHCP 分配地址的 Docker 容器

android - CONNECTIVITY_ACTION 的 BroadcastReceiver 在 intent.getExtras() 中总是返回 null

iphone - 使用 AirDrop 发送和接收文件

ios - 如何将 StatusBar 样式更新为自定义转换的一部分

ios - 在 iOS 13 上隐藏导航栏分隔线

ios - 禁用连接警报 Multipeer Connectivity

ios - 如何根据 UITableview 的行数调整 UIScrollView/ContainerView 的高度?

iPhone 缩小了重叠的 Sprite 图像

flash - 是否可以在 Flash/ActionScript 中使用 UDP?