要求
1.群聊系统可以实现服务器端和客户端之间的数据简单通讯(非阻塞)代码实现(服务端)
2.通过系统可以实现多人群聊
【基于netty的构建一个群聊系统】3.服务器端:可以监控用户上线,离线,并实现消息转发功能
4.客户端:通过channel可以无阻塞发送消息给其它所有用户,同时可以接受到其它用户发送的消息(由服务端转发得到)
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 final int PORT=6667;public GroupChatServer(){try{//得到选择器selector=Selector.open();listenChannel=ServerSocketChannel.open();//绑定端口listenChannel.socket().bind(new InetSocketAddress(PORT));//设置非阻塞模式listenChannel.configureBlocking(false);//将该ListenChannel注册到selectorlistenChannel.register(selector, SelectionKey.OP_ACCEPT);}catch (IOException e){e.printStackTrace();}}//读取客户端的消息public void readData(SelectionKey key){//定义一个SocketChannelSocketChannel channel=null;try{channel = (SocketChannel) key.channel();//创建bufferByteBuffer buffer= ByteBuffer.allocate(1024);int count=channel.read(buffer);//根据count的值可以知道有没有督导数据if(count >0){//把缓冲区的数据转变为字符串String msg=new String(buffer.array());//输出该消息System.out.println("客户端"+msg);//向其他的客户端转发给其他的客户端sendInfoToOtherClients(msg,channel);}}catch (Exception e){//一旦读取不到客户端的消息,那么表示这个客户端下线了try{System.out.println(channel.getRemoteAddress()+"离线了");//取消注册key.channel();//关闭通道channel.close();}catch(IOException ex){ex.printStackTrace();}}}//给所有,除了自己channel发送消息publicvoid sendInfoToOtherClients(String msg,SocketChannel channel) throws IOException {System.out.println("服务器消息发送中");//遍历所有for(SelectionKey key:selector.keys()){Channel targetChannel=key.channel();//排除自己if(targetChannel instanceof SocketChannel && targetChannel!=channel){SocketChannel dest=(SocketChannel) targetChannel;ByteBuffer buffer=ByteBuffer.wrap(msg.getBytes());//写入到通道dest.write(buffer);}}}//监听public void listen(){try{while(true){int count =selector.select(2000);if(count>0){//有事件处理Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();while(iterator.hasNext()){SelectionKeykey= iterator.next();//判断不同的事件//监听到acceptif(key.isAcceptable()) {SocketChannel sc=listenChannel.accept();sc.configureBlocking(false);//将该sc注册到Seletorsc.register(selector,SelectionKey.OP_READ);//提示某某上线System.out.println(sc.getRemoteAddress()+"上线");}//监听到发送过来数据if(key.isReadable()){//专门处理客户端的消息}//当前的key删除,防止重复处理iterator.remove();}}}}catch (Exception e){e.printStackTrace();}finally {}}public static void main(String[] args) {//创建一个服务器对象GroupChatServer groupChatServer=new GroupChatServer();groupChatServer.listen();} }
代码实现(客户端) 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; import java.util.Set;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 static void main(String[] args) throws Exception {//启动我们的客户端GroupChatClient chatClient=new GroupChatClient();//启动一个线程//每3秒读取从服务器发送数据new Thread(){public void run(){while(true){chatClient.readInfo();try{Thread.currentThread().sleep(3000);}catch (InterruptedException e){e.printStackTrace();}}}}.start();Scanner scanner = new Scanner(System.in);while(scanner.hasNextLine()){String s=scanner.nextLine();chatClient.sendInfo(s);}}public GroupChatClient()throws Exception{selector=Selector.open();//连接服务器socketChannel=socketChannel.open(new InetSocketAddress("127.0.0.1",POST));//设置非阻塞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){//有事件发生的通道Set<SelectionKey> selectionKeys = selector.selectedKeys();Iterator<SelectionKey> iterator = selectionKeys.iterator();while(iterator.hasNext()){SelectionKey key=iterator.next();if(key.isAcceptable()){SocketChannel sc= (SocketChannel) key.channel();ByteBuffer buffer=ByteBuffer.allocate(1024);//读取sc.read(buffer);String msg=new String(buffer.array());System.out.println(msg.trim());//删除当前的selectKeyiterator.remove();}else{}}}}catch (Exception e){e.printStackTrace();}} }
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- URL和URI之间的区别
- 槐米水的功效与作用,炒槐米的功效与作用
- 任务调度之调度算法
- 明日叶茶的危害,明日叶和白背七的区别
- 别再用 kill -9了,这才是微服务上 下线的正确姿势
- 淡斑的花茶,淡斑茶的特点
- 白芍祛斑的功效与作用,白芍baishao的功效与作用
- 最好的金刚菩提子是哪种,菩提子价格是多少金刚菩提子价格介绍
- 好的金花茶多少钱斤,藤县盛林农业金花茶
- Linux的sz和rz命令