AndrewBourgeois
AndrewBourgeois

Reputation: 2765

SocketChannel.write() in a single thread processing multiple clients

my application has a queue with " outgoing network packets" (A POJO with a ByteBuffer and a SocketChannel) inside, consumed by a single thread that writes the data to the SocketChannel.

I do this to guarantee that every client that should receive packets, gets its turn. This means that SocketChannel.write() writes sequentially to multiple clients (= 1 at a time).

Can anyone tell me what could go wrong working like this? The SocketChannels are created from a ServerSocketChannel, so they're blocking.

I fear that the write() operation could block for 1 client, making the other clients wait...

Upvotes: 0

Views: 707

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533442

If a client socket fails to consume all the data in one write (non-blocking), you could close the client. This will only happen when the buffer fills, so you could increase the send buffer of the socket to a level where you are comfortable doing this.

Upvotes: 0

user207421
user207421

Reputation: 310860

The write() operation can indeed block in blocking mode. If you want fairness and single threading you will have to use non-blocking mode.

Upvotes: 2

Related Questions