Answer the question
In order to leave comments, you need to log in
What I/O streams to use in a client-server application (TCP)?
Good afternoon.
I create a client and server application in Java (TCP protocol).
However, I can't figure out which I/O stream decorators to use in this case.
Now I use ObjectInputStream and ObjectoutputStream respectively, but they are not very convenient.
Tell me which ones are better to use, taking into account the specifics of the task:
The client sends short messages to the server (one object of the String type) as a response receives a short or long message (one object of the String type or an array of them).
The server, respectively, on the contrary.
In C++, I would pass at the beginning of any message its length so that the recipient knows how many times to read the input stream. I think there is a higher level solution in Java.
Answer the question
In order to leave comments, you need to log in
You need Protobuf, BSON, JSON or Thrift.
And forget about ObjectInputStream.
UPD: Comparative benchmark .
I think that your question is not related to Java, but to the specifics of the exchange over TCP: - there are no messages
in TCP , none - it's a stream, ... a pipe of bytes, where it flows into one end, and flows out of the other ;-)
- therefore to send messages you have to format them yourself somehow
- 2 methods are most widely used: a). pass a fixed end-of-message delimiter in the stream (this is how most Internet protocols work), usually "\n\n" (empty string) and b). passing first the length and then the bytes of the body of the message (similar to POST in HTTP).
- sometimes use 2 of these methods together.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question