java:接收UDP数据包以及如何保存它们 [英] java: receiving UDP packet and how to save them

查看:52
本文介绍了java:接收UDP数据包以及如何保存它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 simulink udp block 接收到我的数组中的 udp 数据(byte[] 数据).数据被打包为 int32 ,所以首先我需要解压它.

I receive udp data in my array (byte[] data) from simulink udp block . the data is packed as int32 , so first I need to unpack that .

我不知道如何保存这些数据才能使用它.这些数据是位置,我想使用 OpenGL ES 将它们可视化.我想将数据保存到一个数组中,并能够在下一次迭代中将下一个数据包添加到该数组中,而不是重写整个数组(因为循环)

I don't know how can I save this data to be able to use that . these data are positions and I want to visualize them using OpenGL ES. I want to save the data into an array and be able to add the next packets to that array in next iteration, not rewrite the whole array (because of the loop )

目前数据大小为 1200 * 96 .array 是一个不错的选择吗?

the size of the data is 1200 * 96 for now . is array a good option ?

       int j = 0 ;
  float[] bin1 = new float[(data.length/2)];
  while (j < data.length ) {
    if ( data[2*j+2] >= 0  ) {

      String unhx =(binary(data[2*j+3])+binary(data[2*j+2])+binary(data[2*j+1])+binary(data[2*j]));
      float unbin = ((float)unbinary(unhx)/100);
      bin1[j/2] = unbin;
      print(bin1[1]);
    }

    else if  ( data[2*j+2] < 0 && data[2*j+3] < 0 ) {
      data[2*j] = (byte)(-data[2*j]);
      data[2*j+1] = (byte)(-data[2*j+1]);
      String unhx =(binary(data[2*j+1])+binary(data[2*j]));
      float unbin = ((-1)*(float)unbinary(unhx)/100);
      bin1[j/2] = unbin;
      print(bin1[1]);
      }
      j = j + 2;
  }

现在的问题是,每次新数据包到来时,它都会重写整个 bin1 数组,如何将新数据包添加到 bin1 而不重写整个内容?

now the problem is that each time the new packet comes it rewrites the whole bin1 array , how could I add the new packets to the bin1 not rewrite the whole thing?

推荐答案

回复您编辑的问题:

Melisa,您可能需要考虑使用浮点数列表而不是浮点数数组.否则,您可能会发现自己调整大小并复制数组以添加更多数据.列表/链表理论上有无限长度(你真的不必关心长度),你可以继续添加浮点数.

Melisa, you might want to consider using a List of floats rather than an array of floats. Otherwise, you might find yourself resizing and copying over the array to add on more data. A list/linked list theoretically has unlimited length (you really don't have to concern yourself with the length) and you can just keep adding on floats.

列表

这篇关于java:接收UDP数据包以及如何保存它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆