Java基于NIO實現(xiàn)聊天室功能
更新時間:2021年11月23日 16:32:14 作者:林夕$相心
這篇文章主要為大家詳細介紹了Java基于NIO實現(xiàn)聊天室功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Java基于NIO實現(xiàn)聊天室功能的具體代碼,供大家參考,具體內(nèi)容如下
Sever端
package com.qst.one;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.Channel;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
import java.nio.ByteBuffer;
public class Server {
private static SocketChannel accept;
public static void main(String[] args) {
System.out.println("----服務端啟動---");
try {
// 獲取通道
ServerSocketChannel channel = ServerSocketChannel.open();
// 配置非阻塞模式
channel.configureBlocking(false);
// 綁定連接的端口
channel.bind(new InetSocketAddress(9999));
// 獲取選擇器
Selector selector = Selector.open();
// 注冊通道到選擇器上,開始監(jiān)聽事件
channel.register(selector, SelectionKey.OP_ACCEPT);
// 使用選擇器進行輪詢
while (selector.select() > 0) {
// 獲取到選擇器上所有注冊的通道中已經(jīng)就緒好的事件
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
// 獲取事件
SelectionKey next = iterator.next();
// 判斷事件類型
if (next.isAcceptable()) {
// 獲取通道
accept = channel.accept();
// 獲取當前連接分配地址
SocketAddress address = accept.getLocalAddress();
System.out.println(address + "上線了");
// 切換模式
accept.configureBlocking(false);
// 將通道注冊到選擇器上
accept.register(selector, SelectionKey.OP_READ);
}
// 如果為讀模式
else if (next.isReadable()) {
SocketChannel accept = (SocketChannel) next.channel();
// 讀取事件
ByteBuffer buffer = ByteBuffer.allocate(1024);
int len;
while ((len = accept.read(buffer)) > 0) {
// 開啟讀模式
buffer.flip();
// System.out.println((char)len);
System.out.println(new String(buffer.array(), 0, len));
// 歸位
buffer.clear();
}
}
iterator.remove();
}
}
} catch (Exception e) {
try {
SocketAddress address = accept.getRemoteAddress();
System.out.println(address+"離線了");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
Client端
package com.qst.one;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.SocketChannel;
import java.util.Scanner;
import java.nio.ByteBuffer;
public class Client {
public static void main(String[] args) throws IOException {
SocketChannel channel = SocketChannel.open(new InetSocketAddress("localhost", 9999));
channel.configureBlocking(false);
ByteBuffer buffer = ByteBuffer.allocate(1024);
Scanner sc = new Scanner(System.in);
SocketAddress address = channel.getLocalAddress();
System.out.println(address+"ready~~~");
while(true) {
System.out.print("tim:");
String name = sc.nextLine();
buffer.put(("tim :"+name).getBytes());
buffer.flip();
channel.write(buffer);
buffer.clear();
}
}
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
解決mapper.xml中resultType映射類型的問題
這篇文章主要介紹了解決mapper.xml中resultType映射類型的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Java8中List轉(zhuǎn)Map(Collectors.toMap) 的技巧分享
在最近的工作開發(fā)之中,慢慢習慣了很多Java8中的Stream的用法,很方便而且也可以并行的去執(zhí)行這個流,這篇文章主要給大家介紹了關于Java8中List轉(zhuǎn)Map(Collectors.toMap) 的相關資料,需要的朋友可以參考下2021-07-07
JAVA中使用FTPClient實現(xiàn)文件上傳下載實例代碼
本文給大家介紹如何利用jakarta commons中的FTPClient(在commons-net包中)實現(xiàn)上傳下載文件。非常不錯具有參考借鑒價值,感興趣的朋友一起學習吧2016-06-06

