一個多客戶端聊天室,支持多客戶端聊天,有以下功能:java
下面演示下效果,代碼見附錄!服務器
服務器console網絡
Server is listening now...
服務器consoleapp
Server is listening now...
Server is listening from client :/127.0.0.1:50206
客戶端consoleui
Please input your name.
服務端consolespa
Server is listening now...
Server is listening from client :/127.0.0.1:50206 Server is listening from client /127.0.0.1:50206 data rev is: byr1#@#
客戶端console.net
Please input your name.
byr1
welcome byr1 to chat room! Online numbers:1
服務端console線程
Server is listening now...
Server is listening from client :/127.0.0.1:50206 Server is listening from client /127.0.0.1:50206 data rev is: byr1#@# Server is listening from client :/127.0.0.1:50261 Server is listening from client /127.0.0.1:50261 data rev is: byr2#@#
客戶端byr1 consolerest
Please input your name.
byr1
welcome byr1 to chat room! Online numbers:1 welcome byr2 to chat room! Online numbers:2
客戶端byr2 consolecode
Please input your name.
byr2
welcome byr2 to chat room! Online numbers:2
服務端console
Server is listening now...
Server is listening from client :/127.0.0.1:50206 Server is listening from client /127.0.0.1:50206 data rev is: byr1#@# Server is listening from client :/127.0.0.1:50261 Server is listening from client /127.0.0.1:50261 data rev is: byr2#@# Server is listening from client /127.0.0.1:50206 data rev is: byr1#@#hello byr2, a nice day, isn't it? Server is listening from client /127.0.0.1:50261 data rev is: byr2#@#fine
客戶端byr1 console
Please input your name.
byr1
welcome byr1 to chat room! Online numbers:1 welcome byr2 to chat room! Online numbers:2 hello byr2, a nice day, isn't it? byr2 say fine
客戶端byr2 console
Please input your name.
byr2
welcome byr2 to chat room! Online numbers:2 byr1 say hello byr2, a nice day, isn't it? fine
附錄:server和client代碼
server代碼
package com.huahuiyang.channel; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.Channel; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.nio.charset.Charset; import java.util.HashSet; import java.util.Iterator; import java.util.Set; /** * 網絡多客戶端聊天室 * 功能1: 客戶端經過Java NIO鏈接到服務端,支持多客戶端的鏈接 * 功能2:客戶端初次鏈接時,服務端提示輸入暱稱,若是暱稱已經有人使用,提示從新輸入,若是暱稱惟一,則登陸成功,以後發送消息都須要按照規定格式帶着暱稱發送消息 * 功能3:客戶端登陸後,發送已經設置好的歡迎信息和在線人數給客戶端,而且通知其餘客戶端該客戶端上線 * 功能4:服務器收到已登陸客戶端輸入內容,轉發至其餘登陸客戶端。 * * TODO 客戶端下線檢測 */ public class ChatRoomServer { private Selector selector = null; static final int port = 9999; private Charset charset = Charset.forName("UTF-8"); //用來記錄在線人數,以及暱稱 private static HashSet<String> users = new HashSet<String>(); private static String USER_EXIST = "system message: user exist, please change a name"; //至關於自定義協議格式,與客戶端協商好 private static String USER_CONTENT_SPILIT = "#@#"; private static boolean flag = false; public void init() throws IOException { selector = Selector.open(); ServerSocketChannel server = ServerSocketChannel.open(); server.bind(new InetSocketAddress(port)); //非阻塞的方式 server.configureBlocking(false); //註冊到選擇器上,設置爲監聽狀態 server.register(selector, SelectionKey.OP_ACCEPT); System.out.println("Server is listening now..."); while(true) { int readyChannels = selector.select(); if(readyChannels == 0) continue; Set selectedKeys = selector.selectedKeys(); //能夠經過這個方法,知道可用通道的集合 Iterator keyIterator = selectedKeys.iterator(); while(keyIterator.hasNext()) { SelectionKey sk = (SelectionKey) keyIterator.next(); keyIterator.remove(); dealWithSelectionKey(server,sk); } } } public void dealWithSelectionKey(ServerSocketChannel server,SelectionKey sk) throws IOException { if(sk.isAcceptable()) { SocketChannel sc = server.accept(); //非阻塞模式 sc.configureBlocking(false); //註冊選擇器,並設置爲讀取模式,收到一個鏈接請求,而後起一個SocketChannel,並註冊到selector上,以後這個鏈接的數據,就由這個SocketChannel處理 sc.register(selector, SelectionKey.OP_READ); //將此對應的channel設置爲準備接受其餘客戶端請求 sk.interestOps(SelectionKey.OP_ACCEPT); System.out.println("Server is listening from client :" + sc.getRemoteAddress()); sc.write(charset.encode("Please input your name.")); } //處理來自客戶端的數據讀取請求 if(sk.isReadable()) { //返回該SelectionKey對應的 Channel,其中有數據須要讀取 SocketChannel sc = (SocketChannel)sk.channel(); ByteBuffer buff = ByteBuffer.allocate(1024); StringBuilder content = new StringBuilder(); try { while(sc.read(buff) > 0) { buff.flip(); content.append(charset.decode(buff)); } System.out.println("Server is listening from client " + sc.getRemoteAddress() + " data rev is: " + content); //將此對應的channel設置爲準備下一次接受數據 sk.interestOps(SelectionKey.OP_READ); } catch (IOException io) { sk.cancel(); if(sk.channel() != null) { sk.channel().close(); } } if(content.length() > 0) { String[] arrayContent = content.toString().split(USER_CONTENT_SPILIT); //註冊用戶 if(arrayContent != null && arrayContent.length ==1) { String name = arrayContent[0]; if(users.contains(name)) { sc.write(charset.encode(USER_EXIST)); } else { users.add(name); int num = OnlineNum(selector); String message = "welcome "+name+" to chat room! Online numbers:"+num; BroadCast(selector, null, message); } } //註冊完了,發送消息 else if(arrayContent != null && arrayContent.length >1){ String name = arrayContent[0]; String message = content.substring(name.length()+USER_CONTENT_SPILIT.length()); message = name + " say " + message; if(users.contains(name)) { //不回發給發送此內容的客戶端 BroadCast(selector, sc, message); } } } } } //TODO 要是能檢測下線,就不用這麼統計了 public static int OnlineNum(Selector selector) { int res = 0; for(SelectionKey key : selector.keys()) { Channel targetchannel = key.channel(); if(targetchannel instanceof SocketChannel) { res++; } } return res; } public void BroadCast(Selector selector, SocketChannel except, String content) throws IOException { //廣播數據到全部的SocketChannel中 for(SelectionKey key : selector.keys()) { Channel targetchannel = key.channel(); //若是except不爲空,不回發給發送此內容的客戶端 if(targetchannel instanceof SocketChannel && targetchannel!=except) { SocketChannel dest = (SocketChannel)targetchannel; dest.write(charset.encode(content)); } } } public static void main(String[] args) throws IOException { new ChatRoomServer().init(); } }
client代碼
package com.huahuiyang.channel; 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.nio.charset.Charset; import java.util.Iterator; import java.util.Scanner; import java.util.Set; public class ChatRoomClient { private Selector selector = null; static final int port = 9999; private Charset charset = Charset.forName("UTF-8"); private SocketChannel sc = null; private String name = ""; private static String USER_EXIST = "system message: user exist, please change a name"; private static String USER_CONTENT_SPILIT = "#@#"; public void init() throws IOException { selector = Selector.open(); //鏈接遠程主機的IP和端口 sc = SocketChannel.open(new InetSocketAddress("127.0.0.1",port)); sc.configureBlocking(false); sc.register(selector, SelectionKey.OP_READ); //開闢一個新線程來讀取從服務器端的數據 new Thread(new ClientThread()).start(); //在主線程中 從鍵盤讀取數據輸入到服務器端 Scanner scan = new Scanner(System.in); while(scan.hasNextLine()) { String line = scan.nextLine(); if("".equals(line)) continue; //不容許發空消息 if("".equals(name)) { name = line; line = name+USER_CONTENT_SPILIT; } else { line = name+USER_CONTENT_SPILIT+line; } sc.write(charset.encode(line));//sc既能寫也能讀,這邊是寫 } } private class ClientThread implements Runnable { public void run() { try { while(true) { int readyChannels = selector.select(); if(readyChannels == 0) continue; Set selectedKeys = selector.selectedKeys(); //能夠經過這個方法,知道可用通道的集合 Iterator keyIterator = selectedKeys.iterator(); while(keyIterator.hasNext()) { SelectionKey sk = (SelectionKey) keyIterator.next(); keyIterator.remove(); dealWithSelectionKey(sk); } } } catch (IOException io) {} } private void dealWithSelectionKey(SelectionKey sk) throws IOException { if(sk.isReadable()) { //使用 NIO 讀取 Channel中的數據,這個和全局變量sc是同樣的,由於只註冊了一個SocketChannel //sc既能寫也能讀,這邊是讀 SocketChannel sc = (SocketChannel)sk.channel(); ByteBuffer buff = ByteBuffer.allocate(1024); String content = ""; while(sc.read(buff) > 0) { buff.flip(); content += charset.decode(buff); } //若系統發送通知名字已經存在,則須要換個暱稱 if(USER_EXIST.equals(content)) { name = ""; } System.out.println(content); sk.interestOps(SelectionKey.OP_READ); } } } public static void main(String[] args) throws IOException { new ChatRoomClient().init(); } }