usr999
usr999

Reputation: 156

Sending two lists via socket

Client have button in GUI, when client presses the button, two ArrayList are send to server. How server can separate them? How to send one list I have found this post, but how can i separate two different ArrayList at server? Sending an ArrayList<String> from the server side to the client side over TCP using socket?

Upvotes: 0

Views: 217

Answers (1)

Robadob
Robadob

Reputation: 5349

When using ObjectOutputStream and ObjectInputStream they abstract the protocol so that the objects are automatically 'separated' for you.

You simply need to send the arraylists with; .writeObject(Object o);

myObjectOutputStream.writeObject(myArrayList1);
myObjectOutputStream.writeObject(myArrayList2);

And then receive them with; readObject();

myArrayList1 = (ArrayList<String>)myObjectInputStream.readObject();
myArrayList2 = (ArrayList<String>)myObjectInputStream.readObject();

Just ensure you are reading them in the same order that they are sent.


As a side not, be sure to call .reset() on the ObjectOutputStream if you are writing an updated object to the stream, as it has some form of caching to save it resending objects that have already been written to the stream.

Upvotes: 1

Related Questions