Java sockets: can you send from one stream and receive in another?

This is probably a very simple question, but it's hard for me to find the answer.

Is it possible to write one stream to the Socket output stream while another stream is reading from the Socket input stream?

Edit: This is a client application that accesses an external server. I am not trying to get these topics to talk to each other. Sorry for the ambiguity.

+5
source share
7 answers

Assuming that you mean that threads use the same socket to communicate with an external entity, and not with each other, this is pretty common because you often don't want to block while waiting to read from the socket.

Thread1:

block on read
on recieve message - do something

Thread 2:

do things
on something happening - write to socket

, , .

: concurrency, , , , .

+5

, ", , ", " , , , , ". , โ€‹โ€‹ , , . : . , . , .

. , (POP, IMAP, HTTP ..), , , , ., - , , . , , - - RTMP, . , , -, .

, , , . , , . , , , . -. , / , / - . -, , .

+2

, , .

, , / .

+1

, ? , . ( , ), .

, , , , .

- .

: :

.

: , , , , . , . -, , , .

+1

, . AFAIK , , .

, , .


, . , , .

. http://vanillajava.blogspot.com/2011/11/java-puzzle-single-threaded-client.html

, , BlockingQueue (s) Exchanger ByteBuffers .;)

+1

, ""? - BlockingQueue

0

, , . . , ,

0

All Articles