Reputation: 3059
I have a spreadsheet application in Java, and one of the features it provides (which I developed) is sheet sharing. Basically, anyone can be a client or a server because the app has both server and client code. The user who is the server creates the share, specifies the IP, and then the share is created and active (best case scenario) with the server listening for clients on its IP and selected port.
For auto-discovery, I am using DatagramSockets via UDP broadcasts, while the 'real communication' is TCP-based (after the client is already connected). However, I'm trying to send a List
through that UDP socket and I don't know how to do it. That List
contains the active shares on the server that I need to send to the client so it knows what it can connect to.
It goes like this:
Client -> looks for active servers by sending a packet to the network -> server listens and sends a packet back. This packet should be the List
(if it is possible to send it via these kind of sockets).
Can anyone shed some light on my question? Thank you.
Upvotes: 0
Views: 2314
Reputation: 7297
You can convert your List to a byte[] before sending, and convert it back to a List on the reciever using Java Serialization.
// Sender
List list = new ArrayList();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream outputStream = new ObjectOutputStream(out);
outputStream.writeObject(list);
outputStream.close();
byte[] listData = out.toByteArray();
// Reciever
ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(listData));
list = inputStream.readObject();
Just make sure all the objects you put in your List implements Serializable.
Upvotes: 3