JAVA NIO实现简单聊天室功能
在JAVA NIO(New IO)中,实现简单聊天室功能通常需要以下步骤:
1. 创建ServerSocketChannel/IoServerSocketChannel实例
在JAVA NIO中,ServerSocketChannel和IoServerSocketChannel类分别充当服务器端的套接字通道。需要通过这两个类的实例来处理客户端请求的连接。
示例代码:
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress("127.0.0.1",8080));
2. 创建Selector对象
Selector对象用来统一管理I/O事件的注册、删除以及处理等操作。
示例代码:
Selector selector = Selector.open();
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
3. 监听连接请求
在ServerSocketChannel/IoServerSocketChannel实例中调用accept()方法,通过Selector管理Accept操作。
示例代码:
while(true) {
selector.select();
Set<SelectionKey> selectionKeySet= selector.keys();
for(SelectionKey selectionKey : selectionKeySet) {
if(selectionKey.isAcceptable()) {
SocketChannel channel = ((ServerSocketChannel)selectionKey.channel()).accept();
channel.configureBlocking(false);
channel.register(selector, SelectionKey.OP_READ);
}
}
}
4. 处理连接请求
在连接被建立成功之后,就需要处理请求发送的信息。客户端向服务器请求的是一个聊天室连接,所以需要实现数据的读取、转发、以及广播等功能。
示例代码:
while(true) {
selector.select();
Set<SelectionKey> selectionKeySet= selector.keys();
for(SelectionKey selectionKey : selectionKeySet) {
if(selectionKey.isAcceptable()) {
SocketChannel channel = ((ServerSocketChannel)selectionKey.channel()).accept();
channel.configureBlocking(false);
channel.register(selector, SelectionKey.OP_READ);
}else if(selectionKey.isReadable()) {
SocketChannel channel = (SocketChannel) selectionKey.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
channel.read(byteBuffer);
String message = new String(byteBuffer.array(), StandardCharsets.UTF_8);
System.out.println("receive message:"+message);
for(SelectionKey key : selectionKeySet) {
Channel targetChannel = key.channel();
if(targetChannel instanceof SocketChannel && targetChannel != channel) {
((SocketChannel) targetChannel).write(ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8)));
}
}
}
}
}
示例1:客户端发送信息并广播
public static void main(String[] args) throws IOException, InterruptedException {
SocketChannel socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("localhost", 8080));
socketChannel.configureBlocking(false);
while(!socketChannel.finishConnect() ){//自旋
}
Thread thread = new Thread(() -> {
while(true) {
ByteBuffer buf = ByteBuffer.allocate(1024);
try {
socketChannel.read(buf);
buf.flip();
String data = new String(buf.array()).trim();
System.out.println("服务端回送的数据:"+data);
} catch (IOException e) {
e.printStackTrace();
}
}
});
Scanner scanner = new Scanner(System.in);
thread.start();
String line = "";
while (!line.equals("quit")) {
line = scanner.nextLine();
ByteBuffer buffer = ByteBuffer.wrap(line.getBytes(StandardCharsets.UTF_8));
socketChannel.write(buffer);
}
socketChannel.close();
}
示例2:客户端接收来自服务端的广播信息
public static void main(String[] args) throws IOException {
SocketChannel socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("localhost", 8080));
socketChannel.configureBlocking(false);
while(!socketChannel.finishConnect() ){//自旋
}
ByteBuffer buf = ByteBuffer.allocate(1024);
int count = 0;
while((count = socketChannel.read(buf))>=0) {
buf.flip();
String message = new String(buf.array(),0,count,StandardCharsets.UTF_8);
System.out.println("receive message:"+message);
}
socketChannel.close();
}
以上就是JAVA NIO实现简单聊天室功能的完整攻略。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:JAVA NIO实现简单聊天室功能 - Python技术站