【问题标题】:Calculate delay of messages between two computers running Java program计算运行 Java 程序的两台计算机之间的消息延迟
【发布时间】:2014-03-22 15:57:17
【问题描述】:

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

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

我在字符串本身中设置了发送时间 long sent = System.currentTimeMillis()

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

计算最终延迟

long delay = receive - sent; long finalDelay = TimeUnit.MILLISECONDS.convert(delay, TimeUnit.MILLISECONDS);

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

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

我已将 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));

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

【问题讨论】:

  • 显示您的发送代码和接收代码。一端可能有问题。
  • 嗨,我用发送者和接收者的代码更新了我的问题
  • 我使用了上一个回复 here 中提到的实用程序 ClockSynchro。它每 1 分钟同步一次所有计算机。我注意到在几分钟的顶部延迟是正常的,但偶尔会在分钟结束时出现负值。这很奇怪,可能与硬件有关,但我现在只有这些。

标签: java delay


【解决方案1】:

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

【讨论】:

  • 虽然这并不能真正回答他的问题,但我同意,这就是我会做的。除以二有点随意,因为网络上的时间无论如何都是可变的。
  • @jgitter 这是发送和接收时间的平均值。平均值不是“任意的”。
  • 是的,虽然同意你的建议,但我需要在接收方延迟。使用这种方法,发送者将知道延迟(单向延迟除以 2),因此为了发送该值,我需要向接收者发送一条额外的消息,我不想这样做。
  • @EJP 你不需要捍卫你的荣誉......我只是说你不会确切知道从处理时间开始通过网络发送整个消息需要多长时间因为接收器也在里面。
猜你喜欢
  • 1970-01-01
  • 2014-01-21
  • 1970-01-01
  • 2020-07-03
  • 1970-01-01
  • 2016-04-23
  • 1970-01-01
  • 2015-03-05
  • 1970-01-01
相关资源
最近更新 更多