Search code examples
javanetwork-programmingasynchronousniochannel

Sending File Data To Multiple Clients?


I'm trying to figure out the best way to go about writing data transfer code for a client/server system that handles multiple clients at once.

I'm already keeping a List of clients who connect (I'm using NIO non-blocking framework btw).

Isn't it costly on performance to iterate through every client with each read/write pass and write the buffer data to each channel? Is there a better/more efficient way of doing it?

I've been thinking about dividing up the buffer size based on the number of clients. Is that a viable solution?


Solution

  • Using selectors (as you seem to be doing) really pays when you're handling a really large number of clients (and why optimize for the case in which you don't have a large number of clients ;)

    The bottleneck in such system is rarely the CPU which does the iteration, but the I/O anyway, so I wouldn't worry if I where you.