Can you write to a sockets input and output stream at the same time?

前端 未结 3 1709
南方客
南方客 2021-01-04 08:58

I have a Java application which is Voip. I am using the one socket to send and receive information at the same time via threads. Code is shown below ..

Socke         


        
3条回答
  •  情话喂你
    2021-01-04 09:38

    Yes you can write to a sockets input and output stream at the same time.

    from do-java-sockets-support-full-duplex

    Since the input stream and the output stream are separate objects within the Socket, the only thing you might concern yourself with is, what happens if you had 2 threads trying to read or write (two threads, same input/output stream) at the same time? The read/write methods of the InputStream/OutputStream classes are not synchronized. It is possible, however, that if you're using a sub-class of InputStream/OutputStream, that the reading/writing methods you're calling are synchronized. You can check the javadoc for whatever class/methods you're calling, and find that out pretty quick.

提交回复
热议问题