简体   繁体   English

用Java解析数据报的有效方法

[英]Efficient way to parse a datagram in Java

Right now I am using a socket and a datagram packet. 现在,我正在使用套接字和数据报包。 This program is for a LAN network and sends at least 30 packets a second at 500 bytes maximum. 该程序用于LAN网络,每秒至少发送30个数据包,最大长度为500字节。

this is how I am receiving my data 这就是我接收数据的方式

payload = new String(incomingPacket.getData(), incomingPacket.getOffset(), incomingPacket.getLength(), "UTF-8");

Currently I am using no offset and I parse one by one through each character. 目前,我没有使用偏移量,而是通过每个字符一个一个地解析。 I use the first 2 characters right now to determine what type of message it is but that is subject to change, then I break down variables and seperate the data with an exclamation mark to tell me when the next variable begins. 我现在使用前两个字符来确定它是什么类型的消息,但是该消息可能会更改,然后我分解变量并用感叹号分隔数据,以告诉我下一个变量何时开始。 At the end I parse it and apply it to my program. 最后,我解析它并将其应用于我的程序。 Is there a faster way to break down and interpret datagram packets? 有没有更快的方法来分解和解释数据报包? Will there be a performance difference if I put the length of the variables in the offset. 如果将变量的长度放在偏移量中,会不会有性能差异。 Maybe an example would be useful. 也许一个例子会有用。 Also I think my variables are too small to use StringBuilder so I use normal concatenation. 我也认为我的变量太小而无法使用StringBuilder,因此我使用普通的串联。

What you are talking about here is setting up your own protocol for communication. 您在这里谈论的是建立自己的通信协议。 While I have this as the fourth part of my socket tutorial (I'm currently working on part 3, non-blocking sockets) I can explain some things here already. 虽然这是我的套接字教程的第四部分(我目前正在研究第3部分,非阻塞套接字),但我已经可以在这里解释一些事情了。

There are several ways of setting up such a protocol, depending on your needs. 根据您的需要,可以通过多种方式来设置此类协议。 One way of doing it is having a byte in front of each piece of data declaring the size, in bytes. 一种实现方法是在每个声明数据大小的数据前面都有一个字节,以字节为单位。 That way, you know the length of the byte array containing the next variable value. 这样,您就知道了包含下一个变量值的字节数组的长度。 This makes it easy to read out whole variables in one go via the System.arraycopy method. 这使得通过System.arraycopy方法一次性读取整个变量变得容易。 This is a method I've used before. 这是我以前使用过的方法。 If the object being sent is always the same, this is all you need to do. 如果要发送的对象始终相同,这就是您需要做的所有事情。 Write the variables in a standardized order, add the size of the variable value and you're done. 以标准化顺序编写变量,添加变量值的大小,即可完成操作。

If you have to send multiple types of objects throught the stream you might want to add a bit of meta data. 如果必须在流中发送多种类型的对象,则可能需要添加一些元数据。 This meta data can then be used to tell what kind of object is being sent and the order of the variables. 然后,可以使用此元数据来告知正在发送的对象类型以及变量的顺序。 This meta data can then be put in a header which you add before the actual message. 然后可以将此元数据放在您在实际消息之前添加的标头中。 Once again, the values in the header are preceded by the size byte. 同样,标头中的值前面还有大小字节。

In my tutorial I'll write up a complete code example. 在我的教程中,我将编写一个完整的代码示例。

Don't use a String at all. 根本不要使用String Just process the underlying byte array directly: scan it for delimiters, counts, what have you. 只需直接处理底层的字节数组:扫描它以查找定界符,计数,您所拥有的。 You can use a DataInputStream wrapped around a ByteArrayInputStream wrapped around the byte array if you want an API oriented to Java primitive datatypes. 如果想要面向Java基本数据类型的API,则可以使用包裹在字节数组周围的ByteArrayInputStream包裹的DataInputStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM