从 C++ 中的主机获取延迟

发布于 2024-07-12 22:13:38 字数 107 浏览 10 评论 0 原文

使用 C++,我想使用命令/类来获取 ping 主机的延迟时间以在我的程序中使用。 我尝试使用 ping 命令,但没有简单的方法来收集时间,因为它包含在其他统计信息中。 我希望有一种更简单的方法。

Using C++, I would like to use a command/class to get the latency time from pinging a host to use in my program. I tried using the ping command but there was no easy way to gather the time since its included with other statistical information. I was hoping for an easier approach.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陪你到最终 2024-07-19 22:13:50

如果您使用的是 Windows,则可以使用 win32 api 函数 https://learn.microsoft.com/en-us/windows/win32/api/icmpapi/nf-icmpapi-icmpsendecho2

If you are using Windows, you can use win32 api function https://learn.microsoft.com/en-us/windows/win32/api/icmpapi/nf-icmpapi-icmpsendecho2.

故事灯 2024-07-19 22:13:49

如果您不喜欢正则表达式,您可以随时循环遍历输出,查找 t 后跟 i 后跟 m 后跟 e 后跟 =,每次只单步遍历输出字符串。 将指针存储到 = 之后的 char,然后进一步单步执行并将下一个空格替换为零。 现在您有了一个带有延迟的字符串,可以使用现有的转换函数将其转换为数字。

例如,如果输出是 char* 输出,长度存储在 unsigned int length 中,并且匹配的字符串需要放入 char* match...

for(unsigned int i = 4; i < length; ++i)
{
  if(output[i] == '=')
  {
    if((output[i-4] = 't') && (output[i-3] = 'i') && (output[i-2] = 'm') && (output[i-1] = 'e'))
    {
      match = &(output[i+1]);
      while(output[i] != ' ') ++i;
      output[i] = 0;
      break;
    }
  }
}

不过,正则表达式更好...它们使您的代码看起来更潮且可读。 尽管如果您没有适合它们的库,那么像这样实现它会更快......:)

if you don't like regexes you can always loop through the output, looking for t followed by i followed by m followed by e followed by = by just stepping through the output string one piece at a time. store the pointer to the char after =, then step through futher and replace the next space by a zero. you now have a string with the latency, converting to a number can be done with the existing conversion functions.

e.g. if the output is in char* output with length stored in unsigned int length and the matched string needs to go in char* match...

for(unsigned int i = 4; i < length; ++i)
{
  if(output[i] == '=')
  {
    if((output[i-4] = 't') && (output[i-3] = 'i') && (output[i-2] = 'm') && (output[i-1] = 'e'))
    {
      match = &(output[i+1]);
      while(output[i] != ' ') ++i;
      output[i] = 0;
      break;
    }
  }
}

regexes are nicer though... they make your code look a lot a tider and readable. although if you don't have a library for them it will be faster to just implement it like this... :)

天生の放荡 2024-07-19 22:13:46

ping 工具通常用 C 语言实现,通过发送 ICMP 通过原始套接字回显请求数据包。 系统时间被记录——通常使用gettimeofdayposix -- 当发出 Echo 请求时以及当 回显回复(如果有)被接收以确定往返时间。 您可以使用 套接字

否则,从 ping 系统调用中提取信息可能比您想象的要容易。 关键是打开一个 管道 以允许读取 ping 命令的标准输出(请参阅 popen_popen)。 正则表达式(例如“time=([0-9]*)”)可以是用于提取所需的数据。 如果您没有可用的正则表达式库,那么提取此数据只需要相当简单的字符串操作。 STL 字符串类 提供了几种可能有用的算法。

The ping tool is usually implemented in C and works by sending ICMP Echo request packets over a raw socket,. The system time is recorded -- usually with gettimeofday on under posix -- when the Echo request is made and again when an Echo reply (if any) is received to determine the round-trip time. You can put the same functionality in your C++ application using sockets.

Otherwise, extracting the information from a ping system call is probably easier than you think. The key is to open a pipe to allow reading of the standard output of the ping command (see popen or _popen). A regular expression (e.g. "time=([0-9]*)") could be used to pluck out the desired data. If you don't have a regex library available, then extracting this data only requires fairly trivial string manipulation. The STL string class provides several algorithms that may be of use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文