版权声明:本文为博主原创文章,转载请附上本文链接地址。from : https://blog.csdn.net/lovechris00 https://blog.csdn.net/lovechris00/article/details/82052985
一般在代码中使用命令行,会想到用 NSTask,但使用ping (/sbin/ping
)的时候,NSTask 并没有像终端一样返回,这时需要用到 SimplePing。
SimplePing 官方文档及demo地址:
https://developer.apple.com/library/archive/samplecode/SimplePing/Introduction/Intro.html
官方demo 中已提供iOS 和Mac 端ping 的示例。
Mac demo 中会通过定时器定时发送数据包,并通过代理查看是否接受成功/失败。
在windows 上cmd 输入ping命令,一般会返回四条ping成功的记录,所以在Mac 版本基础上,我加了一个ping 成功次数,和超时时间设置。
我的demo:
https://github.com/MelissaShu/SimplePingHelper
欢迎提出改进建议。