-
-
Save Botffy/3860641 to your computer and use it in GitHub Desktop.
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(); | |
} | |
} |
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.
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.
Where is the client NIO code?
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 .
@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).
Wouldn't the client have the same issues with regards to blocking read and write?
@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.
@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.
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; }
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 :)
how i can do a long polling server with java.nio?
why netty came when java nio is doing same thing...?
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?
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
- You have a huge amount auf clients >20.000
- 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.
why to use nio for doing a chatapplication...when we can simply do it using socket programming easily ?