您现在的位置是:主页 > news > 英文网站怎么建/燕郊今日头条
英文网站怎么建/燕郊今日头条
admin2025/5/2 18:37:21【news】
简介英文网站怎么建,燕郊今日头条,邢台做wap网站价格,网络建设公司名称NIO群聊系统 简单的编写一个群聊系统、实现服务器端和客户端的群聊系统,实现非阻塞方式的数据简单通讯 服务端功能 最基本的当然是注册功能,也就是将serverSocketChannel注册进Selector,Selector负责调度事件、监听、读取客户端发来…
英文网站怎么建,燕郊今日头条,邢台做wap网站价格,网络建设公司名称NIO群聊系统 简单的编写一个群聊系统、实现服务器端和客户端的群聊系统,实现非阻塞方式的数据简单通讯
服务端功能 最基本的当然是注册功能,也就是将serverSocketChannel注册进Selector,Selector负责调度事件、监听、读取客户端发来…
NIO群聊系统
简单的编写一个群聊系统、实现服务器端和客户端的群聊系统,实现非阻塞方式的数据简单通讯
服务端功能
最基本的当然是注册功能,也就是将serverSocketChannel注册进Selector,Selector负责调度事件、监听、读取客户端发来的消息、收到客户端的信息后将其转发到其它客户端,完成群聊功能
客户端功能
连接服务端,注册进Selector、读写消息
工作流程如下:
服务端源码
package com.mylove.nio.groupchat;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注册到selector上,关注accept事件listenChannel.register(selector, SelectionKey.OP_ACCEPT);}catch (IOException e){e.printStackTrace();}}public void listen(){try{//循环处理while (true){int count = selector.select();if(count > 0 ){//遍历得到selectionKey集合Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();while (iterator.hasNext()){SelectionKey selectionKey = iterator.next();//监听到acceptif(selectionKey.isAcceptable()){SocketChannel socketChannel = listenChannel.accept();socketChannel.configureBlocking(false);socketChannel.register(selector,SelectionKey.OP_READ);System.out.println(socketChannel.getRemoteAddress() + " 上线 ");}//通道READ事件if(selectionKey.isReadable()){//处理消息readData(selectionKey);}iterator.remove();}}}}catch (Exception e){e.printStackTrace();}finally {{}}}/*** 读取消息* @param key*/private void readData(SelectionKey key){SocketChannel channel = null;try{//得到Channelchannel = (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("from 客户端:" + msg);//向其他客户端转发消息sendInfoToOtherClient(msg,channel);}}catch (IOException e){try {System.out.println(channel.getRemoteAddress() + " 离线了。。。");//取消注册key.cancel();//关闭通道channel.close();} catch (IOException e1) {e1.printStackTrace();}}}/*** 转发消息给其他其他客户(通道)* @param msg* @param self*/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 byteBuffer = ByteBuffer.wrap(msg.getBytes());dest.write(byteBuffer);}}}public static void main(String[] args) {GroupChatServer chatServer = new GroupChatServer();chatServer.listen();}
}
客户端源码
package com.mylove.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 final String HOST = "127.0.0.1";private final int PORT = 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",PORT));//设置非阻塞socketChannel.configureBlocking(false);//将channel注册到selector上socketChannel.register(selector, SelectionKey.OP_READ);username = socketChannel.getLocalAddress().toString().substring(1);System.out.println(username + " is ok....");}/*** 向服务器发送消息* @param info*/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 socketChannel = (SocketChannel) key.channel();ByteBuffer buffer = ByteBuffer.allocate(1024);//读取socketChannel.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 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);}}}
——————————————————
我和我的技术公众号, 如感兴趣,烦请关注