Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any suggestions on how to wait for an unknown length packet on a socket, and do some processing with its payload bytes as efficiently as possible?

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...

like image 828
V4R15 Avatar asked Feb 14 '12 09:02

V4R15


1 Answers

Do you not just want to create a DatagramSocket and receive DatagramPackets 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.

like image 60
Jon Skeet Avatar answered Nov 08 '22 21:11

Jon Skeet