java - 计算运行Java程序的两台计算机之间的消息延迟

标签 java delay

我有两个程序在两台计算机上运行(假设 A 和 B),A 和 B 都连接到专用 LAN,并且可以轻松 ping/发送/接收数据。

我在计算机 A 上的程序中有一种方法,其中我定期向接收它的 B 发送一个简单的字符串。

我使用字符串本身设置发送时间 长发送 = System.currentTimeMillis()

在计算机 B 中,我有一个正在运行的线程来接收字符串并处理它,收到字符串后,我使用相同的 long receive = System.currentTimeMillis() 获取当前时间,然后使用

计算最终延迟

长延迟=接收-发送; long FinalDelay = TimeUnit.MILLISECONDS.convert(delay, TimeUnit.MILLISECONDS);

问题是,在接收端,我得到了奇怪的值,延迟为负数(大数字和小数字)。

我尝试使用 long delay = receive - sent; 而不使用 TimeUnit 机制,但仍然得到负值。

我已将 A 和 B 同步到时间服务器。两者都运行 Windows 7 操作系统、Java 1.7_21 jdk 和 Netbeans IDE 7.4

我尝试使用 System.nanoTime() 但它给出了相同的结果,而且我认为 System.nanoTime() 不能在我的场景中使用 sendreceive 将获得不同的启动时间,因为它们都运行在不同的 JVM 和不同的机器上。

那么我犯的错误是什么?任何帮助将不胜感激。

更新:使用发送方和接收方代码

发件人

 while (true) {
        try {

            rCon = (RadiogramConnection) Connector.open("radiogram://broadcast:" + HOST_PORT);
            dg = rCon.newDatagram(50);  // only sending 12 bytes of data 
            // Get the current time and sensor reading

            double tempReading = tempSensor.getCelsius();
            long now = VM.getTimeMillis(); // I used System.currentTimeMillis() as well as new Date().gettime() here


            // Flash an LED to indicate a sampling event
            led.setRGB(255, 0, 0);
            led.setOn();
            Utils.sleep(50);
            led.setOff();

            // Package the time and sensor reading into a radio datagram and send it.
            dg.reset();
            dg.writeLong(now);   // I send the time in a datagram
            dg.writeInt((int)tempReading);              
            rCon.send(dg);

            rCon.close();         

            // Go to sleep to conserve battery
            Utils.sleep(SAMPLE_PERIOD - (System.currentTimeMillis() - now));
        } catch (Exception e) {
            System.err.println("Caught " + e + " while collecting/sending sensor sample.");
        }
    }

接收者

public void run()  throws Exception {
    RadiogramConnection rCon;
    Datagram dg;

    try {

        rCon = (RadiogramConnection) Connector.open("radiogram://:" + HOST_PORT);
        dg = rCon.newDatagram(rCon.getMaximumLength());
    } catch (IOException e) {
         System.err.println("setUp caught " + e.getMessage());
         throw e;
    }
    // Main data collection loop
    while (true) {
        try {
            // Read sensor sample received over the radio
            rCon.receive(dg);


            long sendTime = dg.readLong();      // read time of the reading
            int val = dg.readInt();

            long time = System.currentTimeMillis();                

             System.out.println("Original Time : "+sendTime +" ms and Receiving Time : "+time+
                     " ms =:= Temperature value = " + val); 
             System.out.println("The Total delay is: "+(time-sendTime));

如果我在同一台计算机上运行发送器和接收器,那么我会得到预期的延迟值,但在不同的计算机上这很疯狂。

最佳答案

测量请求主机处完整往返、请求和响应的时间,并将其除以二。这样你就可以消除等式中的任何时间偏差。

关于java - 计算运行Java程序的两台计算机之间的消息延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22570574/

相关文章:

swift - UIAlertController 中的倒计时

timer - 在线程组迭代之间添加延迟时间

java - 远程调试器挂起最新版本的 IntelliJ IDEA 2018.3

java - 在as400上开发java程序从db2创建xml文件

jquery - 使用 jQuery 延迟页面重新加载

Jquery 淡出延迟

java - 用于单向 OneToMany 的 JPQL

Java 按特定步长舍入

java - 使用JTree获取Java中每个子类和兄弟类的名称

c# - 在 c# 中使用 WinForm App 进行操作的时间延迟