计算往返时间(RTT)的C程序

PHPz
PHPz 转载
2023-08-25 23:17:10 578浏览

计算往返时间(RTT)的C程序

给定任何网站的URL地址;任务是计算网站的往返时间。

往返时间(RTT)是发送信号所需的总时间或长度,加上接收到该信号的确认所需的时间。此时间还包括信号之间的传播时间。

用户可以通过ping IP地址来确定他/她的往返时间。

往返时间的结果取决于以下原因:

  • 传输介质。
  • 电路中的接口。
  • 从源到目的地的节点数。
  • 流量量。
  • 从源到目的地的物理距离。
  • 传输介质的性质(无线、光纤等)。
  • 请求数量。
  • 电路中的接口。

通常,往返时间的持续时间为毫秒,我们以秒为单位显示输出。

示例

Input: www.tutorialspoint.com
Output: Time taken:0.3676435947418213
Input: www.indiatoday.in
Output: Time taken:0.4621298224721691

我们将使用以下方法来解决给定问题 −

  • 获取要计算RTT(往返时间)的URL的输入字符串。
  • 记录请求URL之前的时间并将其存储到一个变量中。
  • 发送请求。
  • 记录接收到确认后的时间。
  • 比较这两个时间,我们将得到RTT。

算法

Start
   Step 1 -> import time
   Step 2 -> import requests
   Step 3 -> define a function def roundtriptime(url):
      Set t1 = time.time()
      Set req = requests.get(url)
      Set t2 = time.time()
      Set t = str(t2-t1)
      Print Time taken
   Step 4 -> Initialize url = "http://www.tutorialspoint.com"
   Step 5 -> Call function roundtriptime(url)
Stop

示例

import time
import requests
# Function to calculate the roundtriptime
def roundtriptime(url):
   # time when the signal is sent
      t1 = time.time()
      req = requests.get(url)
   # time when the acknowledgement
   # is received
      t2 = time.time()
   # total time taken
      t = str(t2-t1)
      print("Time taken:" + t)
   # url address
      url = "http://www.tutorialspoint.com"
      roundtriptime(url)

输出

Time taken:0.3676435947418213

以上就是计算往返时间(RTT)的C程序的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除