Java sockets support full duplex?

Is it possible for one stream to write to the OutputStream Java Socket and the other to read from the InputStream socket, without the need to synchronize the streams in the socket?

+49
java multithreading sockets
Jun 07 2018-11-11T00:
source share
2 answers

Sure. The exact situation you are describing should not be a problem (reading and writing at the same time).

Typically, the read stream will be blocked if it is not read, and there may be a timeout in the read operation if you have the specified timeout set.

Since the input stream and the output stream are separate objects inside the Socket, the only thing you can worry about is what happens if you had 2 threads trying to read or write (two streams, the same input / output stream) in the same time? The read / write methods of the InputStream / OutputStream classes are not synchronized. It is possible, however, that if you use a subclass of the InputStream / OutputStream class, then the read / write methods you invoke are synchronized. You can check javadoc for any class / methods you call and quickly find this.

+45
Jun 07 2018-11-11T00:
source share

Yes, it is safe.

If you need more than one stream read from an InputStream, you need to be more careful (assuming you read more than one byte at a time).

+9
Jun 07 2018-11-11T00:
source share



All Articles