1、功能介绍
- 基于NIO实现一个群聊系统,实现服务器端和客户端之间的简单通讯,非阻塞方式
- 实现多人群聊,客户端的消息可以无阻塞的发给所有的其他用户,也能接收其它用户的消息
- 服务器端:需要监测用户上线、下线,实现客户端消息转发功能
- 客户端:通过通道无阻塞的发送消息给其它用户,并接收其他用户的消息
2、系统分析
- **服务器端:**服务器启动并监听端口,服务器接收客户端的消息并转发,同时处理客户端上线和离线
- **客户端:**连接服务器,发送消息,接收服务器转发的消息
3、服务器端实现
package NIO.groupchat;
import com.sun.jdi.event.StepEvent;
import javax.swing.event.ListSelectionListener;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
public class GroupChatServer {
private Selector selector;
private ServerSocketChannel listenChannel;
private static int PORT = 6667;
public GroupChatServer(){
try{
selector = Selector.open();
listenChannel = ServerSocketChannel.open();
listenChannel.socket().bind(new InetSocketAddress(PORT));
listenChannel.configureBlocking(false);
listenChannel.register(selector, SelectionKey.OP_ACCEPT);
}catch (IOException e){
e.printStackTrace();
}
}
public void listen(){
try{
while(true){
int count = selector.select();
if(count > 0){
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while(iterator.hasNext()){
SelectionKey key = iterator.next();
if(key.isAcceptable()){
SocketChannel sc = listenChannel.accept();
sc.configureBlocking(false);
sc.register(selector,SelectionKey.OP_READ);
System.out.println(sc.getRemoteAddress()+" 上线");
}
if(key.isReadable()){
readData(key);
}
iterator.remove();
}
}else{
System.out.println("等待...");
}
}
} catch (Exception e){
e.printStackTrace();
}finally {
}
}
private void readData(SelectionKey key) {
SocketChannel channel = null;
try {
channel = (SocketChannel)key.channel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
int count = channel.read(buffer);
if(count > 0){
String msg = new String(buffer.array());
System.out.println("FROM 客户端:"+ msg);
sendInfoToOtherClient(msg,channel);
}
}catch (IOException e){
try {
System.out.println(channel.getRemoteAddress() + "离线了");
key.cancel();
channel.close();
} catch (IOException ee){
ee.printStackTrace();
}
}
}
private void sendInfoToOtherClient(String msg, SocketChannel self) throws IOException{
System.out.println("服务器转发消息中...");
for(SelectionKey key : selector.keys()){
Channel targetChannel = key.channel();
if(targetChannel instanceof SocketChannel && targetChannel != self){
SocketChannel dest = (SocketChannel)targetChannel;
ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
dest.write(buffer);
}
}
}
public static void main(String[] args) {
GroupChatServer groupChatServer = new GroupChatServer();
groupChatServer.listen();
}
}
4、客户端实现
package 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.sql.BatchUpdateException;
import java.util.Iterator;
import java.util.Scanner;
public class GroupChatClient {
private final String HOST = "127.0.0.1";
private final int POST = 6667;
private Selector selector;
private SocketChannel socketChannel;
private String username;
public GroupChatClient() throws IOException {
selector = Selector.open();
socketChannel = socketChannel.open(new InetSocketAddress("127.0.0.1",POST));
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ);
username = socketChannel.getRemoteAddress().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();
}else{
System.out.println("没有可用通道...");
}
} catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
GroupChatClient groupChatClient = new GroupChatClient();
new Thread(){
@Override
public void run(){
while(true){
groupChatClient.readInfo();
try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
}.start();
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextLine()){
String str = scanner.nextLine();
groupChatClient.sendInfo(str);
}
}
}