java使用epoll方式实现一个C/S架构的应用;要求:服务器同时支持多个客户端连接,一个客户端发送消息,服务器转发到其他客户端并回显消息;
时间: 2024-05-01 10:19:35 浏览: 208
首先,Java中使用NIO来实现epoll方式,NIO使用Selector作为IO多路复用器,实现了异步非阻塞IO操作。下面是一个简单的C/S架构的应用的示例代码,实现了一个简单的聊天室。
服务端代码:
```java
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.util.Iterator;
public class Server {
public static void main(String[] args) throws IOException {
// 创建ServerSocketChannel,用于监听客户端连接请求
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress("localhost", 8000));
serverSocketChannel.configureBlocking(false);
// 创建Selector,用于监听所有Channel上的事件
Selector selector = Selector.open();
// 将ServerSocketChannel注册到Selector上,监听OP_ACCEPT事件
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
int readyChannels = selector.select();
if (readyChannels == 0) continue;
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
iterator.remove();
handle(key);
}
}
}
private static void handle(SelectionKey key) throws IOException {
if (key.isAcceptable()) {
// 如果是OP_ACCEPT事件,表示有新的客户端连接请求
ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(false);
// 将SocketChannel注册到Selector上
socketChannel.register(key.selector(), SelectionKey.OP_READ);
// 给客户端发送欢迎消息
socketChannel.write(Charset.defaultCharset().encode("欢迎来到聊天室!\n"));
} else if (key.isReadable()) {
// 如果是OP_READ事件,表示客户端发送了消息
SocketChannel socketChannel = (SocketChannel) key.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
StringBuilder content = new StringBuilder();
while (socketChannel.read(byteBuffer) > 0) {
byteBuffer.flip();
content.append(Charset.defaultCharset().decode(byteBuffer));
}
// 如果读到的消息不为空,则广播给其他客户端
if (content.length() > 0) {
System.out.println("收到来自" + socketChannel.getRemoteAddress() + "的消息:" + content);
for (SelectionKey selectionKey : key.selector().keys()) {
if (selectionKey.channel() instanceof SocketChannel && selectionKey.channel() != socketChannel) {
SocketChannel targetChannel = (SocketChannel) selectionKey.channel();
targetChannel.write(Charset.defaultCharset().encode(socketChannel.getRemoteAddress() + ":" + content));
}
}
}
}
}
}
```
客户端代码:
```java
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws IOException {
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
socketChannel.connect(new InetSocketAddress("localhost", 8000));
while (!socketChannel.finishConnect()) {}
// 启动一个新的线程,用于接收服务器的消息
new Thread(() -> {
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
try {
while (socketChannel.read(byteBuffer) > 0) {
byteBuffer.flip();
System.out.print(Charset.defaultCharset().decode(byteBuffer));
byteBuffer.clear();
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
// 从标准输入读入消息并发送到服务器
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.equals("quit")) {
break;
}
socketChannel.write(Charset.defaultCharset().encode(line + "\n"));
}
socketChannel.close();
}
}
```
这样,我们就实现了一个基于epoll方式的C/S架构的应用,可以同时支持多个客户端连接,一个客户端发送消息,服务器会将消息转发到其他客户端并回显消息。
阅读全文