iOS - Ping 超时 [英] iOS - Ping with timeout

查看:34
本文介绍了iOS - Ping 超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Apple 的Simple Ping"示例,它几乎具有我需要的所有功能,但我不知道在哪里可以设置每个数据包的超时时间.似乎不可能,因为用于将数据写入套接字的函数没有任何超时参数.有没有人有想法改变这个应用程序以获得像在 windows ping 命令中一样设置超时的能力?超时是指等待响应时间过长后每个发送的数据包被丢弃的时间.

I'm using Apple's "Simple Ping" example and it has almost all features that I need, but I don't know where I can set timeout of each packet. It seems that it isn't possible because function that is used to write data to socket doesn't have any timeout parameters. Does anybody have idea to change this app to get ability to set timeout like in windows ping command? By timeout I mean time for each packet sent to be discarded after waiting for response too long.

Windows ping 命令 - 我需要超时:

Windows ping command - timeout I need to have:

"-w Timeout : 指定等待给定 Echo Request 消息对应的 Echo Reply 消息被接收的时间量,以毫秒为单位.如果在超时时间内没有收到 Echo Reply 消息,显示请求超时"错误消息.默认超时为 4000(4 秒)."

"-w Timeout : Specifies the amount of time, in milliseconds, to wait for the Echo Reply message that corresponds to a given Echo Request message to be received. If the Echo Reply message is not received within the time-out, the "Request timed out" error message is displayed. The default time-out is 4000 (4 seconds)."

我正在使用的简单 Ping 代码:http://developer.apple.com/library/mac/#samplecode/SimplePing/简介/Intro.html

Simple Ping code I'm using: http://developer.apple.com/library/mac/#samplecode/SimplePing/Introduction/Intro.html

推荐答案

Apple 示例代码:

Apple sample code:

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

更改超时时间:

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]
);

请参阅 Apple 的文档:setsockopt

See Apple's docs: setsockopt

来自上面引用的文档:
SO_SNDTIMEO 是为输出操作设置超时值的选项.它接受一个 struct timbal 参数,其中包含用于限制等待输出操作完成的秒数和微秒数.如果发送操作阻塞了这么长时间,它会返回一个部分计数或错误 EWOULDBLOCK 如果没有发送数据.在当前的实现中,每次额外的数据被传送到协议时,这个定时器都会重新启动,这意味着限制适用于大小从低水位线到高水位线的输出部分.

From the above referenced doc:
SO_SNDTIMEO is an option to set a timeout value for output operations. It accepts a struct timbal parameter with the number of seconds and microseconds used to limit waits for output operations to complete. If a send operation has blocked for this much time, it returns with a partial count or with the error EWOULDBLOCK if no data were sent. In the current implementation, this timer is restarted each time additional data are delivered to the protocol, implying that the limit applies to output portions ranging in size from the low-water mark to the high-water mark for output.

这篇关于iOS - Ping 超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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