user2619631
user2619631

Reputation: 555

What is the best way to send multiple data values through a socket?

so I have a Raspberry Pi that tracks a few temp sensor outputs and controls heaters to meet user defined setpoints. I'm now trying to create an app that talks with the pi so I can see the setpoints and actual temperature reading from my phone. In doing so, I was hoping to use a multicast so that I could eventually expand it to use more than one device. I want to eventually send a few pieces of information including a name for each device and the data values for the various sensors. I was kind of hoping that Java would have something I could send with a datagram packet that contains all of that info. I was thinking something similar to structs in C++ (This is one of my first attempts at Java), but I'm having trouble, since structs don't exist.

I could have a class that is entirely public, but I would then have to send that through the datagram, which seems pretty strange. Is there a better way to do this in Java?

I don't really have code to post, since this is sort of a design issue, but any code samples illustrating possible solutions would be greatly appreciated.

Thanks for your help!

Edit: I should add that while I am writing this for android right now, I intend to do a similar thing on a PC.. I'm not sure how they differ or if they even would in this case, but I figured I'd throw it out there.

Upvotes: 0

Views: 228

Answers (1)

user207421
user207421

Reputation: 310883

Use a DataOutputStream and its primitive-writing methods in conjunction with a ByteArrayOutputStream, and wrap the bytes of he latter in a DatagramPacket. At the receiver, do the converse.

Upvotes: 1

Related Questions