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

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

问题描述

我正在使用Apple的简单Ping示例,它几乎具有我需要的所有功能,但我不知道在哪里可以设置每个数据包的超时。似乎不可能,因为用于向socket写入数据的函数没有任何超时参数。有没有人有想法改变这个应用程序以获得设置超时的能力,如在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超时:指定等待与要接收的给定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代码I'使用:
http://developer.apple.com/library/ mac /#samplecode / SimplePing / Introduction / 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天全站免登陆