Last active
May 28, 2020 09:00
-
-
Save kojilin/e17c73f5fd34e30b3f56160952e2379f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.IOException; | |
| import java.net.InetSocketAddress; | |
| import java.net.ServerSocket; | |
| import java.nio.ByteBuffer; | |
| import java.nio.channels.SelectionKey; | |
| import java.nio.channels.Selector; | |
| import java.nio.channels.ServerSocketChannel; | |
| import java.nio.channels.SocketChannel; | |
| import java.util.Iterator; | |
| import java.util.Set; | |
| // Just for demo of nio | |
| public class NioServer { | |
| public static void main(String[] args) throws IOException { | |
| final int port = 5555; | |
| final String host = "localhost"; | |
| new NioServer(host, port).run(); | |
| } | |
| String host; | |
| int port; | |
| NioServer(String host, int port) { | |
| this.host = host; | |
| this.port = port; | |
| } | |
| public void run() throws IOException { | |
| ServerSocketChannel serverChannel = null; | |
| Selector selector = null; | |
| try { | |
| serverChannel = ServerSocketChannel.open(); | |
| final ServerSocket s = serverChannel.socket(); | |
| final InetSocketAddress address = new InetSocketAddress(port); | |
| s.bind(address); | |
| serverChannel.configureBlocking(false); | |
| selector = Selector.open(); | |
| serverChannel.register(selector, SelectionKey.OP_ACCEPT); | |
| } catch (Exception ex) { | |
| ex.printStackTrace(); | |
| } | |
| while (selector.select() > 0) { | |
| final Set<SelectionKey> readKeys = selector.selectedKeys(); | |
| final Iterator<SelectionKey> iterator = readKeys.iterator(); | |
| while (iterator.hasNext()) { | |
| final SelectionKey key = iterator.next(); | |
| iterator.remove(); | |
| try { | |
| if (key.isAcceptable()) { | |
| final ServerSocketChannel server = (ServerSocketChannel) key.channel(); | |
| final SocketChannel client = server.accept(); | |
| System.out.println("Accepted connection from :" + client); | |
| client.configureBlocking(false); | |
| final SelectionKey clientKey = client.register(selector, SelectionKey.OP_READ); | |
| final ByteBuffer buffer = ByteBuffer.allocate(100); | |
| clientKey.attach(buffer); | |
| } else if (key.isReadable()) { | |
| final SocketChannel client = (SocketChannel) key.channel(); | |
| final ByteBuffer buffer = (ByteBuffer) key.attachment(); | |
| if (client.read(buffer) < 0) { | |
| client.close(); | |
| continue; | |
| } | |
| buffer.flip(); | |
| client.write(buffer); | |
| buffer.compact(); | |
| } | |
| } catch (Exception ex) { | |
| ex.printStackTrace(); | |
| key.cancel(); | |
| try { | |
| key.channel().close(); | |
| } catch (Exception exx) { | |
| exx.printStackTrace(); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment