public class NioServer {
public static void main(String[] args) throws Exception {
new NioServer().start();
}
private void start() throws Exception {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(9999));
serverSocketChannel.configureBlocking(false);
Selector selector = Selector.open();
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT, "服務器");
while (selector.select() > 0) {
Set <SelectionKey> selectionKeys = selector.selectedKeys();
Iterator <SelectionKey> iterator = selectionKeys.iterator();
while (iterator.hasNext()) {
SelectionKey next = iterator.next();
iterator.remove();
if (next.isAcceptable()) {
acceptable(selector, serverSocketChannel);
}
if (next.isReadable()) {
broadcast(next, selector);
}
}
}
}
private void broadcast(SelectionKey selectionKey, Selector Selector) throws Exception {
SocketChannel channel = (SocketChannel) selectionKey.channel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
String str = "";
while (channel.read(buffer) > 0) {
buffer.flip();
str += Charset.forName("unicode").decode(buffer);
buffer.clear();
}
String finalStr = str;
Selector.keys().forEach(t -> {
SelectableChannel selectableChannel = t.channel();
if (selectableChannel instanceof SocketChannel && !selectableChannel.equals(channel)) {
SocketChannel socketChannel = (SocketChannel) selectableChannel;
try {
socketChannel.write(Charset.forName("unicode").encode(finalStr));
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void acceptable(Selector selector, ServerSocketChannel serverSocketChannel) throws Exception {
SocketChannel channel = serverSocketChannel.accept();
channel.configureBlocking(false);
SelectionKey register = channel.register(selector, SelectionKey.OP_READ);
channel.write(Charset.forName("unicode").encode("您已加入聊天室"));
}
}
public class NioClient {
public static void main(String[] args) throws Exception {
new NioClient().start();
}
private void start() throws Exception {
SocketChannel channel = SocketChannel.open(new InetSocketAddress("localhost", 9999));
Selector selector = Selector.open();
channel.configureBlocking(false);
channel.register(selector, SelectionKey.OP_READ, this.getClass().getSimpleName());
ChatOut(channel);
while (selector.select() > 0) {
Set <SelectionKey> selectionKeys = selector.selectedKeys();
Iterator <SelectionKey> iterator = selectionKeys.iterator();
if (iterator.hasNext()) {
SelectionKey selectionKey = iterator.next();
iterator.remove();
if (selectionKey.isReadable()) {
brodacast(selectionKey, selector);
}
}
}
}
private void ChatOut(SocketChannel channel) {
ExecutorService pool = Executors.newFixedThreadPool(1);
pool.submit(() -> {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String str = scanner.next();
try {
channel.write(Charset.forName("unicode").encode(str));
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void brodacast(SelectionKey selectionKey, Selector selector) throws IOException {
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
Object attachment = selectionKey.attachment();
System.out.println(attachment);
ByteBuffer buffer = ByteBuffer.allocate(1024);
String str = "";
while (socketChannel.read(buffer) > 0) {
buffer.flip();
str += Charset.forName("unicode").decode(buffer);
buffer.clear();
}
System.out.println(str);
}
}