详细版 Java中用网络通信实现聊天室私聊和群聊的功能

Java中用网络通信实现聊天室私聊和群聊的功能三、启动流程四、效果展示
一、结构图
二、代码实现 ①类
package com.allen;import java.io.*;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/*** 要求实现谁和谁聊天的提示* @author Administrator**/public class Server {//群发//定义一个集合,用于存储所有客户端的输出流List allPw = new ArrayList<>();//用于根据用户名存储客户端输出流Map map = new HashMap();//创建线程池对象//ExecutorService pool = Executors.newCachedThreadPool();public static void main(String[] args) {try {//向系统申请端口号 端口号是系统中所有程序没有使用过的端口,才能使用成功//端口号的范围:0-65535之间 0-1023之间是系统端口ServerSocket server = new ServerSocket(8088);System.out.println("服务端开启成功!!!");Server s = new Server();while(true){System.out.println("等待客户端连接:");//阻塞方法 accept():等待客户端的连接Socket accept = server.accept();System.out.println("和客户端连接成功!!!");//获取IP地址InetAddress address = accept.getInetAddress();String ip = address.getHostAddress();System.out.println(ip);Thread t = new Thread(s.new ClientHandler(accept));t.start();}} catch (IOException e) {e.printStackTrace();}}class ClientHandler implements Runnable{private Socket socket;public ClientHandler(Socket socket){this.socket = socket;}public void run(){try{InputStream is = socket.getInputStream();InputStreamReader isr = new InputStreamReader(is,"GBK");BufferedReader br = new BufferedReader(isr);OutputStream os = socket.getOutputStream();OutputStreamWriter osw = new OutputStreamWriter(os,"GBK");PrintWriter pw = new PrintWriter(osw,true);//一个客户端连接服务器,则把输出流存入到集合中String str = null;//用户名String name = br.readLine();allPw.add(pw);map.put(name, pw);while((str = br.readLine())!=null){if(str.equals("1")){//私聊工作//需要私聊的对象str = br.readLine();//私聊对象的输出流PrintWriter p = map.get(str);str = br.readLine();p.println(str);}else{//群发System.out.println("");str = br.readLine();for(PrintWriter p:allPw){if(p == pw){continue;}p.println(str);}}}}catch(IOException e){e.printStackTrace();}}}}
②类

详细版  Java中用网络通信实现聊天室私聊和群聊的功能

文章插图
package com.allen;import java.io.*;import java.net.Socket;import java.util.Scanner;public class Client {public static void main(String[] args) {//连接服务器try {System.out.println("连接服务端");Socket socket = new Socket("localhost",8088);System.out.println("连接服务端成功");Thread t1 = new Thread(new ClientWriteData(socket));Thread t2 = new Thread(new ClientReadData(socket));t1.start();t2.start();} catch (IOException e) {e.printStackTrace();}}}class ClientWriteData implements Runnable{private Scanner scan;private Socket socket;public ClientWriteData(Socket socket){this.socket = socket;}public void run(){try {OutputStream out = socket.getOutputStream();OutputStreamWriter osw;osw = new OutputStreamWriter(out,"GBK");PrintWriter pw = new PrintWriter(osw,true);System.out.println("请输入用户名:");scan = new Scanner(System.in);String str0 = scan.next();pw.println(str0);while(true){System.out.println("请选择私聊或者群发:1表示私聊\t2表示群发");int n = scan.nextInt();pw.println(n);switch(n){case 1:System.out.println("请输入私聊对象: ");String name = scan.next();pw.println(name);System.out.println("私聊的信息:");String str = scan.next();pw.println(str);break;case 2:System.out.println("请输入群发消息:");System.out.println(str0+"正在发消息:");String str1 = scan.next();pw.println(str1);break;default:System.out.println("退出消息发送:");System.exit(0);}}} catch (IOException e) {e.printStackTrace();}}}class ClientReadData implements Runnable{private Socket socket;public ClientReadData(Socket socket){this.socket = socket;}public void run(){try {InputStream is = socket.getInputStream();InputStreamReader isr = new InputStreamReader(is,"GBK");BufferedReader br = new BufferedReader(isr);String str = null;while((str = br.readLine())!=null){System.out.println(str);}} catch (IOException e) {System.err.println("服务端已断开!!!");e.printStackTrace();}}}