日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java中nio怎么实现聊天,JAVA-NIO实现聊天室详细代码说明

發布時間:2023/12/13 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中nio怎么实现聊天,JAVA-NIO实现聊天室详细代码说明 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

JAVA-NIO實現聊天室詳細代碼說明

JAVA-NIO實現聊天室詳細代碼說明

github源碼:https://github.com/JolyouLu/JAVAIO.git

src\main\java\com\JolyouLu\nio\groupchat 文件夾下

public class GroupChatServer {

//定義屬性

private Selector selector;

private ServerSocketChannel listenChannel;

private static final int PORT = 6666;

//構造器 完成初始化工作

public GroupChatServer() {

try {

//得到選擇器

selector = Selector.open();

//初始化ServerSocketChannel

listenChannel = ServerSocketChannel.open();

//綁定端口

listenChannel.socket().bind(new InetSocketAddress(PORT));

//設置非阻塞模式

listenChannel.configureBlocking(false);

//將來listenChannel注冊到Selector

listenChannel.register(selector, SelectionKey.OP_ACCEPT);

}catch (IOException e){

e.printStackTrace();

}

}

//監聽

public void listen(){

try {

//循環監聽

while (true){

int count = selector.select(2000); //阻塞2秒監聽,通道有沒有事件發生

if (count > 0){ //返回>0 有事件要處理

//遍歷selectedKeys集合

Iterator iterator = selector.selectedKeys().iterator();

while (iterator.hasNext()){

//獲取SelectionKey

SelectionKey key = iterator.next();

if (key.isAcceptable()){ //如果通道發生,客戶端連接事件

//為連接的客戶端,生成socketChannel

SocketChannel socketChannel = listenChannel.accept();

//切換非阻塞模式

socketChannel.configureBlocking(false);

//把socketChannel注冊到selector中,并監聽讀事件

socketChannel.register(selector,SelectionKey.OP_READ);

//提示客戶端連接上了

System.out.println(socketChannel.getRemoteAddress() + " 客戶端 上線");

}

if (key.isReadable()){//如果通道發生,可讀事件

//處理讀

readData(key);

}

//清理讀取的selectedKeys容器 防止重復處理

iterator.remove();

}

}

}

}catch (Exception e){

e.printStackTrace();

}finally {

}

}

//讀取客戶端消息

private void readData(SelectionKey key){

//定義一個SocketChannel

SocketChannel channel = null;

try {

//取到關聯的channel

channel = (SocketChannel) key.channel();

//創建buffer

ByteBuffer buffer = ByteBuffer.allocate(1024);

int count = channel.read(buffer);

//根據count的值做處理

if (count > 0){ //讀取到數據

//把緩沖區的數據轉字符串

String msg = new String(buffer.array(), "GBK");

//輸出消息

System.out.println("from 客戶端:"+ msg);

//向其它客戶端轉發消息

sendInfoToOtherClients(msg,channel);

}

}catch (IOException e){

try {

System.out.println(channel.getRemoteAddress() + " 離線了..");

//取消注冊

key.cancel();

//關閉通道

channel.close();

} catch (IOException ioException) {

ioException.printStackTrace();

}

}

}

//轉發消息給其它客戶端(channel)

private void sendInfoToOtherClients(String msg,SocketChannel self) throws IOException {

System.out.println("服務器轉發消息中...");

//遍歷 所有的注冊到Selector的SocketChannel排查self

for (SelectionKey key : selector.keys()) {

//取出通道

Channel targetChannel = key.channel();

//targetChanneld的類型是SocketChannel,并且targetChannel不是自己

if (targetChannel instanceof SocketChannel && targetChannel != self){

//轉型

SocketChannel dest = (SocketChannel)targetChannel;

//將來msg 存到buffer

ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes("GBK"));

//將來buffer的數據寫入通道

dest.write(buffer);

}

}

}

public static void main(String[] args) {

//初始化服務器對象

GroupChatServer chatServer = new GroupChatServer();

chatServer.listen();

}

}

客戶端

public class GroupChatClient {

//定義相關屬性

private final String HOST = "127.0.0.1"; //服務器IP

private final int PORT = 6666; //服務器端口

private Selector selector;

private SocketChannel socketChannel;

private String username;

//構造器,初始化

public GroupChatClient() {

try {

//得到選擇器

selector = Selector.open();

//連接服務

socketChannel = SocketChannel.open(new InetSocketAddress(HOST,PORT));

//設置非阻塞

socketChannel.configureBlocking(false);

//將來socketChannel注冊到Selector,關注讀事件

socketChannel.register(selector, SelectionKey.OP_READ);

//得到username

username = socketChannel.getLocalAddress().toString().substring(1);

System.out.println(username + " is ok ...");

}catch (Exception e){

e.printStackTrace();

}

}

//向服務器發送消息

public void sendInfo(String info){

info = username + " 說:" + info;

try {

socketChannel.write(ByteBuffer.wrap(info.getBytes("GBK")));

}catch (IOException e){

e.printStackTrace();

}

}

//讀取從服務器端回復的消息

public void readInfo(){

try {

int readChannels = selector.select(2000);

if (readChannels > 0){//有可用的通道

Iterator iterator = selector.selectedKeys().iterator();

while (iterator.hasNext()){

SelectionKey key = iterator.next();

if (key.isReadable()){

//得到相關的通道

SocketChannel socketChannel = (SocketChannel) key.channel();

//得到一個Buffer

ByteBuffer buffer = ByteBuffer.allocate(1024);

//buffer 讀取通道數據

socketChannel.read(buffer);

//把讀到緩沖區的數據轉成字符串

String msg = new String(buffer.array());

System.out.println(msg.trim());

}

iterator.remove();

}

}

}catch (IOException e){

e.printStackTrace();

}

}

public static void main(String[] args) {

//啟動客戶端

GroupChatClient chatClient = new GroupChatClient();

//啟動一個線程,每隔開3秒讀取服務器發送的數據

new Thread(new Runnable() {

@Override

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);

}

}

}

測試

設置啟動參數,當前類可以同時運行多個

JAVA-NIO實現聊天室詳細代碼說明相關教程

總結

以上是生活随笔為你收集整理的java中nio怎么实现聊天,JAVA-NIO实现聊天室详细代码说明的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。