比较数据包时间戳和查找延迟

时间:2016-02-28 15:03:37

标签: java

所以我使用DataOutputStreams来保存数据包的当前时间戳。

客户端:

    byte[] tempBuffer = recorder.getBlock();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    dos.write(tempBuffer);
    dos.writeLong(System.currentTimeMillis());
    dos.flush();
    DatagramPacket sendPacket = new DatagramPacket(baos.toByteArray(), baos.size(), clientIP, PORT);

服务器端:

    DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
    receiving_socket.receive(packet);
    long received  =System.currentTimeMillis();
    byte[] udpPacketBytes = packet.getData();
    ByteArrayInputStream baos = new ByteArrayInputStream(udpPacketBytes);
    DataInputStream dos = new DataInputStream(baos);
    long timeSend = dos.readLong();
    System.out.println(received- timeSend);

所以我想找出数据包发送到实际检索时的时间差异。

我目前得到的输出如下:

-144336931610443142
-6413368508412665479
-9223293613480097670
-3242630961109421321
-1657385879957442570
1445848312512800754
4030252469527785967
3672742989582189418
2660264248110580060
8210679245661657192
-7276041996288176391
-7783862498534639127
-6198549255261902107

这不可能是对的吗?如何在发送数据包之前收到数据包?我确定我编码错了,任何指针都会很棒,谢谢

编辑:

收到的输出:

System.out.println(received);

-

    1456672303102
    1456672303222
    1456672303222
    1456672303222
    1456672303223
    1456672303348
    1456672303348
    1456672303348
    1456672303348
    1456672303474

1 个答案:

答案 0 :(得分:0)

没有为接收器端的writeLong()所需的额外字节做准备。需要一个520的字节数组,我有一个512的字节数组