Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.net.InetSocketAddress;
- import java.net.ServerSocket;
- import java.nio.ByteBuffer;
- import java.nio.channels.ClosedChannelException;
- import java.nio.channels.SelectionKey;
- import java.nio.channels.Selector;
- import java.nio.channels.ServerSocketChannel;
- import java.nio.channels.SocketChannel;
- import java.util.Iterator;
- /**
- * @author marlonyao<yaolei135@gmail.com>
- *
- */
- public class EchoServer {
- public static int DEFAULT_PORT = 7777;
- public static void main(String[] args) throws IOException {
- System.out.println("Listening for connection on port " + DEFAULT_PORT);
- Selector selector = Selector.open();
- initServer(selector);
- while (true) {
- selector.select();
- for (Iterator<SelectionKey> itor = selector.selectedKeys().iterator(); itor.hasNext();) {
- SelectionKey key = (SelectionKey) itor.next();
- itor.remove();
- try {
- if (key.isAcceptable()) {
- ServerSocketChannel server = (ServerSocketChannel) key.channel();
- SocketChannel client = server.accept();
- System.out.println("Accepted connection from " + client);
- client.configureBlocking(false);
- SelectionKey clientKey = client.register(selector, SelectionKey.OP_WRITE|SelectionKey.OP_READ);
- ByteBuffer buffer = ByteBuffer.allocate(100);
- clientKey.attach(buffer);
- }
- if (key.isReadable()) {
- SocketChannel client = (SocketChannel) key.channel();
- ByteBuffer output = (ByteBuffer) key.attachment();
- client.read(output);
- }
- if (key.isWritable()) {
- // System.out.println("is writable...");
- SocketChannel client = (SocketChannel) key.channel();
- ByteBuffer output = (ByteBuffer) key.attachment();
- output.flip();
- client.write(output);
- output.compact();
- }
- } catch (IOException e) {
- key.cancel();
- try { key.channel().close(); } catch (IOException ioe) { }
- }
- }
- }
- }
- private static void initServer(Selector selector) throws IOException,
- ClosedChannelException {
- ServerSocketChannel serverChannel = ServerSocketChannel.open();
- ServerSocket ss = serverChannel.socket();
- ss.bind(new InetSocketAddress(DEFAULT_PORT));
- serverChannel.configureBlocking(false);
- serverChannel.register(selector, SelectionKey.OP_ACCEPT);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement