Skip to content

Instantly share code, notes, and snippets.

@Botffy
Created October 9, 2012 18:45
Show Gist options
  • Save Botffy/3860641 to your computer and use it in GitHub Desktop.
Save Botffy/3860641 to your computer and use it in GitHub Desktop.
simple Java NIO chat server
package niochat;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.io.IOException;
import java.util.*;
public class NiochatServer implements Runnable {
private final int port;
private ServerSocketChannel ssc;
private Selector selector;
private ByteBuffer buf = ByteBuffer.allocate(256);
NiochatServer(int port) throws IOException {
this.port = port;
this.ssc = ServerSocketChannel.open();
this.ssc.socket().bind(new InetSocketAddress(port));
this.ssc.configureBlocking(false);
this.selector = Selector.open();
this.ssc.register(selector, SelectionKey.OP_ACCEPT);
}
@Override public void run() {
try {
System.out.println("Server starting on port " + this.port);
Iterator<SelectionKey> iter;
SelectionKey key;
while(this.ssc.isOpen()) {
selector.select();
iter=this.selector.selectedKeys().iterator();
while(iter.hasNext()) {
key = iter.next();
iter.remove();
if(key.isAcceptable()) this.handleAccept(key);
if(key.isReadable()) this.handleRead(key);
}
}
} catch(IOException e) {
System.out.println("IOException, server of port " +this.port+ " terminating. Stack trace:");
e.printStackTrace();
}
}
private final ByteBuffer welcomeBuf = ByteBuffer.wrap("Welcome to NioChat!\n".getBytes());
private void handleAccept(SelectionKey key) throws IOException {
SocketChannel sc = ((ServerSocketChannel) key.channel()).accept();
String address = (new StringBuilder( sc.socket().getInetAddress().toString() )).append(":").append( sc.socket().getPort() ).toString();
sc.configureBlocking(false);
sc.register(selector, SelectionKey.OP_READ, address);
sc.write(welcomeBuf);
welcomeBuf.rewind();
System.out.println("accepted connection from: "+address);
}
private void handleRead(SelectionKey key) throws IOException {
SocketChannel ch = (SocketChannel) key.channel();
StringBuilder sb = new StringBuilder();
buf.clear();
int read = 0;
while( (read = ch.read(buf)) > 0 ) {
buf.flip();
byte[] bytes = new byte[buf.limit()];
buf.get(bytes);
sb.append(new String(bytes));
buf.clear();
}
String msg;
if(read<0) {
msg = key.attachment()+" left the chat.\n";
ch.close();
}
else {
msg = key.attachment()+": "+sb.toString();
}
System.out.println(msg);
broadcast(msg);
}
private void broadcast(String msg) throws IOException {
ByteBuffer msgBuf=ByteBuffer.wrap(msg.getBytes());
for(SelectionKey key : selector.keys()) {
if(key.isValid() && key.channel() instanceof SocketChannel) {
SocketChannel sch=(SocketChannel) key.channel();
sch.write(msgBuf);
msgBuf.rewind();
}
}
}
public static void main(String[] args) throws IOException {
NiochatServer server = new NiochatServer(10523);
(new Thread(server)).start();
}
}
@abhi9090
Copy link

abhi9090 commented Jun 3, 2015

why to use nio for doing a chatapplication...when we can simply do it using socket programming easily ?

@Botffy
Copy link
Author

Botffy commented Sep 8, 2015

Because nio is non-blocking, so doesn't require additional threads. A socket-based chat would require as many threads as there are users, adding a significant overhead, while a nio chat would always need but one thread, making it a lot more scalable, as the overhead of threading may get really significant.

@jorkey
Copy link

jorkey commented Mar 26, 2016

Why you are not check number of bytes written on channel.write()?
Some bytes may be not be written because no free space in the socket's output buffer.

@babs93
Copy link

babs93 commented Apr 4, 2016

Where is the client NIO code?

@Botffy
Copy link
Author

Botffy commented May 17, 2016

@babs93: There isn't one! A client does not really need the non-blocking io, it can happily devote a thread to its single connection. IIRC I used this with plain old telnet, by the way.

@jorkey: Fair enough, sorry about that. Feel free to fork and stuff? :)

@abh1sh2k
Copy link

abh1sh2k commented Jul 4, 2016

How to send message to only a particular socket instead of broadcasting to all ? For example : there are three client socket A , B and C . how to send message from socket A to B ? in simple io server socket it is easy to send .

@Botffy
Copy link
Author

Botffy commented Jul 12, 2016

@abh1sh2k: Notice the broadcast function (line 85) and how it iterates over all channels, writing the message into each. To target a specific channel, just choose that one (according to whatever criteria).

@tinkuge
Copy link

tinkuge commented Nov 26, 2016

Wouldn't the client have the same issues with regards to blocking read and write?

@Botffy
Copy link
Author

Botffy commented Dec 13, 2016

@tinkuge Not really. It can happily afford a thread to reading and another to writing, if that's what you mean. Why couldn't it, it's just two threads.

The problem with a server with blocking IO is, it needs to maintain a thread to communicate with each client. It just doesn't scale, due to the overhead of managing and maintaining the threads.

Btw, I totally wonder why this gist attracts so many comments.

@MarcusTalbotNL
Copy link

@Botffy probably because it's one of the first Google results when searching for "Java chat server github", queries like that.

I do believe I'm gonna stick with the more robust multi-threaded servermodel though.

@alexpmorris
Copy link

alexpmorris commented Sep 10, 2017

need to catch exception around read() to properly handle client disconnects (around line 65):

    try {
        while( (read = ch.read(buf)) > 0 ) {
            buf.flip();
            byte[] bytes = new byte[buf.limit()];
            buf.get(bytes);
            sb.append(new String(bytes));
            buf.clear();
        }
    } catch (Exception e) { key.cancel(); read = -1; }

@delicacyyy
Copy link

delicacyyy commented Oct 9, 2017

fix with echo "foo" | netcat localhost 10523

if(sb.length() == 0) {
	msg = key.attachment()+" left the chat.\n";
	ch.close();
}
else if (read == -1) {
	msg = key.attachment()+": "+sb.toString();
	ch.close();
}

try it :)

@Davidc2525
Copy link

how i can do a long polling server with java.nio?

@vinaymcu
Copy link

why netty came when java nio is doing same thing...?

@thanhdn
Copy link

thanhdn commented Apr 16, 2020

I have a stupid question:
As we know we use NIO in case there is a lot of connections and we use Selector to manage these connections via Channels
So, when we should use Stream IO?

@Samyssmile
Copy link

Because nio is non-blocking, so doesn't require additional threads. A socket-based chat would require as many threads as there are users, adding a significant overhead, while a nio chat would always need but one thread, making it a lot more scalable, as the overhead of threading may get really significant.

Not necessarily. In actual measurements in Java on Linux, multithreaded
classic I/O designs outperform NIO by 30% or so. [see@Java Network Programming Book]

Java NIO outperforms a classic I/O design only if

  1. You have a huge amount auf clients >20.000
  2. Interval between data packages to send is very short.

Conclusion: You need a Server for >20.000 Clients with high frequently communication.

But as Tutorial your Chat Server Example is good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment