Best way to send ArrayList <String []> over TCP in Java?
I am writing a client / server application in Java, and I use TCP to transfer the data that I store in an ArrayList (i.e., an ArrayList of string arrays).
What is the best way to transfer this data from one to another? Should I make one long line and use PrintWriter println () or is there a better way?
Many thanks!
To add a little scaffman answer:
OutputStream socketStream = ...
GZIPOutputStream objectOutput = new GZIPOutputStream(new ObjectOutputStream(socketStream));
objectOutput.writeObject(myDataList);
And on the client:
InputStream socketStream = ...
ObjectInputStream objectInput = new ObjectInputStream(new GZIPInputStream(socketStream));
ArrayList<type> a = objectInput.readObject();
Assuming that the client and server are both written in Java, and assuming that you are sticking to raw sockets, rather than a higher level framework structure:
OutputStream socketStream = ...
ObjectOutput objectOutput = new ObjectOutputStream(socketStream);
objectOutput.writeObject(myDataList);
, ObjectInputStream .
, java.io.Serializable.
You can see Serialization . However, you could just design your own format for such a simple case. Personally, I prefer bencoding . However, the minimal effort (and least error prone) solution is serialization.