关于如何在套接字上等待未知长度的数据包,并尽可能有效地对其有效负载字节进行一些处理,有什么建议吗?
我需要编写一个 UDP 服务器,它将等待来自不相关设备(最多 10000 个)的数据包,并定期发送小数据包;对有效负载进行一些处理并将结果写入 SQL 中。现在我已经通过 jdbc 完成了 SQL 部分,但是有效负载字节一直困扰着我,我应该如何访问它们?到目前为止,我已经将有效负载映射到字符串,然后将字符串转换为十六进制(两个十六进制字符代表一个字节)。我知道有更好的方法可以做到这一点,但我不知道......
I need to write an UDP server which will wait for packets from uncorrelated devices (max 10000 of them) sending small packets periodically; do some processing with the payload and write the results on SQL. Now I'm done with the SQL part through jdbc, but the payload bytes keep bugging me, how should I access them? Until now I've worked with the payload mapped to a string and then converting the string to hex (two hex chars representing one byte). I'm aware that there's a better way to do this but I don't know it...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否只想创建一个
DatagramSocket
并在其上接收DatagramPacket
?您需要根据用于创建数据包的缓冲区来指定数据包的最大长度,但随后您将能够找出实际发送了多少数据使用
getLength()
在数据包中。有关更多详细信息和示例,请参阅 Java 教程。
Do you not just want to create a
DatagramSocket
and receiveDatagramPacket
s on it?You need to specify a maximum length of packet by virtue of the buffer you use to create it, but then you'll be able to find out how much data was actually sent in the packet using
getLength()
.See the Java Tutorial for more details and an example.