实现多个用户之间的类似于QQ的聊天(仅文字信息)
package xiangmu; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.HashMap; public class QQServer { public static void main(String[] args) { //声明存放所有人的socket集合 HashMap<String,Socket> hm=new HashMap<>(); try { //8000端口监听 ServerSocket ss=new ServerSocket(8000); System.out.println("服务器正在8000端口监听......."); while(true) { Socket s =ss.accept(); MyService t = new MyService(); //将HashMap的引用传入服务线程 t.setHashMap(hm); t.setSocket(s); t.start(); } } catch (Exception e) { e.printStackTrace(); } } }
package xiangmu; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.util.HashMap; public class MyService extends Thread { private Socket s; private HashMap<String,Socket> hm; public void setSocket(Socket value) { s = value; } public void setHashMap(HashMap<String, Socket> hm) { this.hm=hm; } @Override public void run() { try { // 接收用户名和密码 InputStream is = s.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String uandp = br.readLine(); //检查 //System.out.println(uandp); //拆分用户名和密码 String u = uandp.split("%")[0]; String p = uandp.split("%")[1]; OutputStream os = s.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(os); PrintWriter pw = new PrintWriter(osw,true); //文件验证 File f= new File("user.txt"); FileReader fr = new FileReader(f); BufferedReader br1 = new BufferedReader(fr); boolean b = false; while(br1.ready()) { String userpass = br1.readLine(); String user = userpass.split("%")[0]; String pass = userpass.split("%")[1]; if (u.equals(user) && p.equals(pass)) { b = true; break; } } if (b) { //发送正确信息到客户端 pw.println("ok"); //将本人的名字发送给其他用户 for(Socket ts:hm.values()) { OutputStream tos = ts.getOutputStream(); OutputStreamWriter tosw = new OutputStreamWriter(tos); PrintWriter tpw = new PrintWriter(tosw,true); tpw.println("add%"+u); } //将其他人的名字发送给自己 for(String tu:hm.keySet()) { pw.println("add%"+tu); } //将本人的用户名存入到HashMap hm.put(u, s); //不断接收客户端发送过来的信息 while(true) { String message = br.readLine(); if(message.equals("{exit}")) //将该用户从HashMap中删除 { hm.remove(u); //通知所有人,本用户退出 for(Socket ts:hm.values()) { OutputStream tos = s.getOutputStream(); OutputStreamWriter tosw = new OutputStreamWriter(tos); PrintWriter tpw = new PrintWriter(tosw,true); tpw.print("exit%"+u); } return; } System.out.println(message); } } else{ //发送错误的信息到客户端 pw.println("error"); } } catch (Exception e) { } } }
在MyService中接收并发送验证信息,发送到QQ,界面
这是登录界面,如果输入错误账户或者密码,会弹出用户密码错误窗口,在这里使用了这个语句
JOptionPane.showMessageDialog(this, "用户名密码错误");
登录正确后
登录到界面,多账号登录会在分组自动上线显示账号
点击打开聊天窗口你,实现互发信息,显示在显示面板
下一步期望:
1、可以现在用户之间的文件传输
2、能够发送简单的表情
3、界面优化
时间: 2024-10-10 03:23:22