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!

+3
source share
5 answers

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();
+3
source

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.

+11

, JSON. . json.org JSON = Javascript Object Notation. , Javascript, json.jar /.

+1

-, ​​ Apache CXF. JAXB XML Beans.

0

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.

0
source

Source: https://habr.com/ru/post/1727114/


All Articles