1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| import java.io.IOException; import java.net.InetSocketAddress; 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.nio.charset.Charset; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.Set;
public class Server {
public static void main(String[] args) throws Exception { int port = 8001; Selector selector = Selector.open(); ServerSocketChannel socketChannel = ServerSocketChannel.open(); socketChannel.configureBlocking(false); socketChannel.socket().bind(new InetSocketAddress(port), 1024); socketChannel.register(selector, SelectionKey.OP_ACCEPT); while (true) { System.out.println("Waiting..."); selector.select(1000); Set<SelectionKey> selectionKeySet = selector.selectedKeys(); Iterator<SelectionKey> iterator = selectionKeySet.iterator();
while (iterator.hasNext()) { SelectionKey key = iterator.next(); iterator.remove(); try { handleInput(selector, key); } catch (Exception e) { if (key != null) { key.cancel(); if (key.channel() != null) { key.channel().close(); } } } } try { Thread.sleep(500); } catch (Exception e) { e.printStackTrace(); } } }
public static void handleInput(Selector selector, SelectionKey key) throws IOException { DateFormat normal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if (key.isValid()) { if (key.isAcceptable()) { ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel(); SocketChannel socketChannel = serverSocketChannel.accept(); socketChannel.configureBlocking(false); socketChannel.register(selector, SelectionKey.OP_READ); System.out.println(normal.format(new Date())+" Connecting successfully " + socketChannel.socket().getInetAddress() + ":" + socketChannel.socket().getPort()); } if (key.isReadable()) { SocketChannel socketChannel = (SocketChannel) key.channel(); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); int readBytes = socketChannel.read(byteBuffer); if (readBytes > 0) { byteBuffer.flip(); byte[] bytes = new byte[byteBuffer.remaining()]; byteBuffer.get(bytes); String message = new String(bytes, "UTF-8"); System.out.println(normal.format(new Date()) + " Receive from " + socketChannel.socket().getInetAddress() + ":" + socketChannel.socket().getPort() + " Message:" + message); broadcastClient(message, selector, socketChannel); } else if (readBytes < 0) { key.cancel(); socketChannel.close(); } } } }
private static void broadcastClient(String message, Selector selector, SocketChannel socketChannel) throws IOException { Set<SelectionKey> keys = selector.keys(); Iterator<SelectionKey> iterator = keys.iterator(); while (iterator.hasNext()) { SelectionKey key = iterator.next(); if (key.channel() != socketChannel && key.channel() instanceof SocketChannel) { ((SocketChannel) key.channel()).write(Charset.forName("UTF-8").encode(message)); } } } }
|