【发布时间】: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