Filipe Santos
Filipe Santos

Reputation: 1671

Java NIO2 AIO - TCP Chat Server

i am playing arround with Java NIO2 and i am trying to code a TCP Chat Server based on AsynchronousServerSocketChannel.

To Programm a simple ECHO-Server was no problem and worked flawlessly. Now i am trying to accept many clients and to broadcast incomming messages to all connected clients, but i am facing problems.

Because it got a bit long i posted the code here in pastbin

Until now i am able to broacast one message, after that something goes wrong and after awhile i get the following error:

Exception in thread "pool-1-thread-13" java.nio.channels.WritePendingException

Upvotes: 1

Views: 3133

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533660

Exception in thread "pool-1-thread-13" java.nio.channels.WritePendingException

This means you attempted to write with out waiting for the previous write to complete. When you call write() with NIO2 you get a Future which you can wait on to check that it has finished.

I wouldn't recommend using NIO2 unless you have infiniband (which is what it was designed for) NIO2 is at least twice as complicated as NIO to use and I wouldn't even suggest using NIO unless you are using simple blocking NIO or a library like netty or mina.

For a chat program I would just use plain NIO or even IO. Or you could re-use one of the many, many chat libraries which already exist and work. ;)

Upvotes: 3

Related Questions