java控制臺輸出版多人聊天室
本文實例為大傢分享瞭java控制臺輸出版多人聊天室的具體代碼,供大傢參考,具體內容如下
多人聊天室就要用到網絡以及多線程以及輸入輸出流的知識
多人聊天室就是多個人同時聊天,一個發一條信息所有人都是可以接收得到.
接下來我們捋清楚一下思路
在這裡面服務器充當瞭一個轉發的作用,服務器要做的事情是,得到客戶寫進緩沖流裡面的信息,再把得到的信息再寫進其他客戶端的緩沖流裡面,得到其他用戶的緩沖流我們可以通過集合把用戶的全部Socket對象寫進集合裡面,客戶端把客戶想要發送的信息寫進緩沖流裡,等待客戶端發來的消息,一邊等由一邊發送消息,這個是屬於並發的一個操作,所以讀與寫可以單獨開個線程來進行發送消息.
下面是客戶端的代碼
import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.List; public class Server { public static List<Socket> links = new ArrayList<>(); //用來存儲客戶端的socket對象 public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(6666); while(true){ Socket accept = serverSocket.accept(); links.add(accept); //隻要有人連接就可以把他添加進socket對象集合裡 ServerThread serverThread = new ServerThread(accept); //開啟多線程,實現多人聊天的功能 serverThread.start(); } } //因為頻繁的對輸入輸出流進行資源的關閉,所以寫瞭個關閉資源的工具類 import java.io.Closeable; import java.io.IOException; public class CloseUtil { public static void close(Closeable... closeables){ for (Closeable closeable : closeables) { if (closeable!=null) { try { closeable.close(); } catch (IOException e) { e.printStackTrace(); } } } } } //多線程的代碼如下 import java.io.*; import java.net.Socket; import java.util.List; public class ServerThread extends Thread { private Socket socket; private boolean flag = true; public ServerThread(String name, Socket socket) { super(name); this.socket = socket; } @Override public void run() { while(flag){ sendother(received()); //轉發功能 } } private void sendother(String str) { List<Socket> links = Server.links; //因為在寫客戶端的時候是socket的集合是 靜態的屬性,所以可以直接通過類型.屬性直接獲得 for (Socket link : links) { if (link!=socket) send(str,link); //遍歷整個對象轉發給除當前對象的其他socket對象 } } private void send(String str,Socket socket) { BufferedWriter bw = null; try { OutputStream outputStream = socket.getOutputStream(); bw = new BufferedWriter(new OutputStreamWriter(outputStream)); bw.write(str); bw.newLine(); bw.flush(); } catch (IOException e) { flag = false; CloseUtil.close(bw,socket); } } private String received(){ BufferedReader br = null; try { InputStream inputStream = socket.getInputStream(); br = new BufferedReader(new InputStreamReader(inputStream)); return br.readLine(); } catch (IOException e) { flag = false; CloseUtil.close(br,socket); } return null; } }
下面是客戶端的代碼
import java.io.IOException; import java.net.Socket; import java.util.Scanner; public class Client { public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); System.out.print("請輸入您的名字:"); String name = input.next(); Socket socket = new Socket("127.0.0.1", 6666); //127.0.0.1是指的是本機 Send send = new Send(socket,name); //收發分別開兩個線程,可以收發並發執行 Receive receive = new Receive("",socket); send.start(); receive.start(); } } //發線程 import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.Socket; import java.util.Scanner; public class Send extends Thread { private Socket socket; private boolean flag = true; public Send(Socket socket,String name) { super(name); this.socket = socket; } @Override public void run() { while(flag){ send(); //一直循環保證客戶可以一直發 } } private void send() { Scanner input = new Scanner(System.in); BufferedWriter bw= null; try { OutputStream outputStream = socket.getOutputStream(); bw = new BufferedWriter(new OutputStreamWriter(outputStream)); bw.write(this.getName()+"說:"+input.next()); // 當前線程名+在客戶端在控制臺輸出的一並發給服務端 bw.newLine(); bw.flush(); } catch (IOException e) { flag = false; //出現異常的時候關閉循環,防止陷入死循環 CloseUtil.close(bw,socket); } } } //收線程 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Socket; public class Receive extends Thread { private Socket socket; private boolean flag = true; public Receive(String name, Socket socket) { super(name); this.socket = socket; } @Override public void run() { while (flag){ System.out.println(received()); //循環保證能一直接收服務器發來的消息 } } public String received(){ BufferedReader br =null; try { InputStream inputStream = socket.getInputStream(); br = new BufferedReader(new InputStreamReader(inputStream)); String line = br.readLine(); return this.getName()+line; } catch (IOException e) { flag = false; CloseUtil.close(br,socket); } return ""; } }
以上就是簡易版聊天室,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。