客户端
package com.darwin.netty.nio.groupchat;
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.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;
public class GroupChatClient {
private Selector selector;
private SocketChannel socketChannel;
private final static int SERVER_PORT = 6667;
private final static String SERVER_ADDRESS = "127.0.0.1";
private String userName;
public GroupChatClient () throws IOException {
selector = Selector.open();
socketChannel = SocketChannel.open(new InetSocketAddress(SERVER_ADDRESS, SERVER_PORT));
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ);
userName = socketChannel.getLocalAddress().toString().substring(1);
System.out.println(userName + " is Ok");
}
public void sendInfo (String info) {
info = userName + " : " + info;
try {
socketChannel.write(ByteBuffer.wrap(info.getBytes()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void readInfo () {
try {
int readChannels = selector.select();
if(readChannels > 0) {
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
if(key.isReadable()) {
SocketChannel sc = (SocketChannel) key.channel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
sc.read(buffer);
String msg = new String(buffer.array());
System.out.println(msg.trim());
}
}
iterator.remove();
}
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
GroupChatClient client = new GroupChatClient();
new Thread(() -> {
for (; ; ) {
client.readInfo();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
client.sendInfo(s);
}
}
}
服务端
package com.darwin.netty.nio.groupchat;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.Set;
public class GroupChatServer {
private Selector selector;
private ServerSocketChannel serverSocketChannel;
private final static int PORT = 6667;
public GroupChatServer() {
try {
selector = Selector.open();
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(PORT));
serverSocketChannel.configureBlocking(false);
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void listen() {
try {
for (; ; ) {
int select = selector.select();
if (select > 0) {
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
if (key.isAcceptable()) {
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ);
System.out.println(socketChannel.getRemoteAddress() + " 上线");
}
if (key.isReadable()) {
readData(key);
}
iterator.remove();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void readData(SelectionKey key) {
SocketChannel socketChannel = null;
try {
socketChannel = (SocketChannel)key.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
int read = socketChannel.read(byteBuffer);
if (read > 0) {
String msg = new String(byteBuffer.array());
System.out.println("from 客户端: " + msg);
sendOtherClient(msg, socketChannel);
}
} catch (IOException ex) {
try {
System.out.println(socketChannel.getRemoteAddress() + " 离线了..");
key.cancel();
socketChannel.close();
}catch (IOException e2) {
e2.printStackTrace();;
}
}
}
private void sendOtherClient(String msg, SocketChannel socketChannel) throws IOException {
Set<SelectionKey> keys = selector.keys();
for (SelectionKey key:keys) {
SelectableChannel selectableChannel = key.channel();
if (selectableChannel instanceof SocketChannel && selectableChannel != socketChannel) {
SocketChannel channel = (SocketChannel)selectableChannel;
channel.write(ByteBuffer.wrap(msg.getBytes()));
}
}
}
public static void main(String[] args) {
GroupChatServer server = new GroupChatServer();
server.listen();
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-8724.html