【问题标题】:How to display transmission rate periodically in Mbps, the average for the last second?如何以Mbps为单位定期显示传输速率,最后一秒的平均值?
【发布时间】:2021-12-29 10:26:47
【问题描述】:

我尝试寻找如何在通过 UDP 协议发送数据包(65000 字节)的 Java 程序中应用公式速度 = 传输的数据/持续时间。问题是我似乎不知道如何量化用于计算、转换和总结在最后一秒或 1 秒内传输的数据的操作。我曾尝试使用 Timer 和 TimerTrack 并将数据传输到 run() 函数中,但随后它会引发 IOException。我也考虑过延迟,但这只会将其中断 X ms

这里是代码

公共类 UdpSnd {

公共 UdpSnd() { }

static public void main(String args[]) 抛出异常 {

  // first argument - destination address
  InetAddress target = InetAddress.getByName(args[0]);

  // second argument destination port
  int port = Integer.parseInt(args[1]);

  DatagramSocket socket = new DatagramSocket();
  byte[] buf = new byte[65000];
  double sum = 0;
  double megabits = 0;

  while (true) {

     byte[] lineBytes = buf;

     DatagramPacket pkt = new DatagramPacket(lineBytes, lineBytes.length, target, port);
    // start
     megabits = lineBytes.length * 0.000008; // converting bytes to Megabits, also Bytes * 8 / 1000 * 1000
     sum = sum + megabits; // summing up the values to calculate the average 
     socket.send(pkt);
    // end --> should be the block of code that runs for 1 second to calculate average Mbps value for the last second
    
    // I want to output that periodically:
    // System.out.println(sum + " Mbps"); - not the correct way 

    sum = 0;

  }

} }

非常感谢您的宝贵时间。

【问题讨论】:

    标签: java networking udp bandwidth


    【解决方案1】:

    一种可能性是启动一个周期性的任务来打印和计算总和。但是,如果输出间隔选择的非常短且数据速率非常高,则由于 Java 任务处理,计算的传输速率不太准确。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-23
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多