Permanent I / O flow

I communicate with the server, and ideally I would like the input stream and the output stream to always go up. I get unsolicited answers, so I should always be ready to receive data on the input stream.

Before I dive into much more, I must say that any connection I make must support SSL.

The problem I am facing is when I open the HttpUrLConnection and send stuff from the output stream, it will not send the actual output until I open the input stream. If I open the input stream, it kills the output stream, and it seems that there is no need to restore it after closing the stream.

What would be the best way to realize my desired behavior? If it is not possible to support both streams simultaneously on the same port, is there a way I could have two separate streams that share a connection through a kind of port lock / switch?

Is there a library to send output without killing the output stream to signal the completion of my request?

+4
source share
2 answers

Http is a single request protocol.

You cannot (if websocket) create a persistent connection using http (s). You need a different protocol.

You can use a basic socket. It works mostly as an http connection, except that the URL starts with socket: //.

You just open the connection, and then you can get the input stream and output stream and use them.

+2
source

- My best bet would be to use java.nio package .

- Use the SocketChannel from the java.nio package, we can set the SocketChannel to non-blocking mode . When you do this, you can call connect() , read() and write() in asynchronous mode .

+1
source

All Articles