【问题标题】:How to read the delay stored in the Datagram packet in the receiver using UDP如何使用UDP读取接收器中存储在数据报包中的延迟
【发布时间】:2018-08-14 18:33:04
【问题描述】:

我正在使用 UDP 向我的接收器发送序列码,现在我想测量发送和接收数据包之间的延迟。唯一的问题是我无法弄清楚如何在序列号之后从数据包中读取我的延迟。

发件人

    byte [] packetToSend = new byte[520];
    int seqNo = 0;

    while (seqNo < 100) {
        try {

            //Read in a audio data from the recorder
            byte[] buffer = recorder.getBlock();
            ByteBuffer header = ByteBuffer.allocate(524); 

            long sentMillis = System.currentTimeMillis();


           header.put(buffer); 
           header.putInt(seqNo);
           header.putLong(sentMillis); 



            //Make a DatagramPacket from it, with client address and port 
            number
            DatagramPacket packet = new DatagramPacketheader.array(), 
             header.array().length, clientIP, PORT);

            //Send it
            sending_socket.send(packet);

接收者:

         try {

            byte[] buffer = new byte[520];

            DatagramPacket packet = new DatagramPacket(buffer, 0, 520);




            receiving_socket.receive(packet);
           // int seqNo = ByteBuffer.wrap(buffer).getInt(512);
            long sentMillis = ByteBuffer.wrap(buffer).getLong(508); 
            long receivedMillis = System.currentTimeMillis();
            long delay = receivedMillis - sentMillis; 
            System.out.println(delay); 

到目前为止,我得到了像 1498894798102 这样的数字,我猜我是从 ByteBuffer.wrap 行中的错误索引中读取的。

【问题讨论】:

    标签: java network-programming udp client-server buffer


    【解决方案1】:

    分配一个更大的ByteBuffer 并继续使用各种get()(以及发送时put())方法。丢失所有System.arraycopy() 调用。

    【讨论】:

      猜你喜欢
      • 2020-08-29
      • 2011-02-10
      • 1970-01-01
      • 1970-01-01
      • 2012-06-19
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 2017-12-09
      相关资源
      最近更新 更多