Talking about the thread-per-connection model, each connection is supposed to have one single thread. This thread is used to read data from the stream. However, since I'm using a blocking I/O, I can't send anything while it's trying to read something.
Apparently Minecraft uses two threads per connection, one for reading and one for writing. Is that something I should do too or how should I be implementing this?
As far as I know, sending data also blocks, so I can't just send it from the tick thread, right?
So once again, what I want to know is:
Thanks in advance.
In a simple ping-pong style network dialog, a single thread operating some kind of state-machine will be sufficient.
If, however, data needs to be sent and/or received asynchronously and not in direct response to the last piece of data received or sent, one thread per direction is sensible and not a bad solution in any way.
Non-blocking IO, as provided by Java's NIO
, is advantageous for high-performance, high-throughput applications like heavily loaded web servers etc. where it is not preferrable to have one or two threads per connection, which may total in thousands of threads running concurrently on the server. On the client side not much is to be gained by using non-blocking IO, and multi-threading is the way to go if you don't expect to maintain hundreds of connections simultaneously.