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?
Asked Answered
T

1

6

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

Thyrotoxicosis answered 14/2, 2012 at 9:51 Comment(2)
Why not store the bytes as bytes? e.g. with a BLOBStripling
why do don't save the byte[] from the DatagramPacket's as blobs on your databaseWardell
F
8

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.

Frisco answered 14/2, 2012 at 9:56 Comment(2)
I do have a byte array of length 1400, and I use it for the DatagramPacket but when i print the packet contents it displays a padded packet (zeros after the received data)Thyrotoxicosis
@V4R15: Sounds like you're ignoring getLength(), which should tell you how much of the data you should actually be interested in.Frisco

© 2022 - 2024 — McMap. All rights reserved.