Java QQ聊天工具
发光吖 人气:0Java实现简单的类似QQ聊天工具,供大家参考,具体内容如下
所使用到的知识点:
- java socket编程之TCP协议
- java Swing
- 简单的java多线程
运行截图:
服务器端实现:
package tcp_server; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Iterator; import java.util.Map; import java.util.Set; /** * @author 乔澳 * @version 1.0 * @title: ServerThread * @projectName 20200222TCPServer * @description: * @date 2020/2/27 8:58 */ public class ServerThread extends Thread{ private User user; public ServerThread(User user){ this.user = user; } @Override public void run() { Map<String, User> userMap = Server.userMap; Set<String> keys = userMap.keySet(); try { BufferedReader reader = new BufferedReader(new InputStreamReader(user.getSocket().getInputStream())); while (true) { String value = reader.readLine();// 客户端发来的一句话 String[] valueAndUser = value.split("@"); //群聊 if (valueAndUser.length == 1) { Iterator<String> it = keys.iterator(); while (it.hasNext()) { String key = it.next(); User u = userMap.get(key); PrintWriter pw = new PrintWriter(u.getSocket().getOutputStream()); pw.println(user.getUsername() + ":" + value + "(来自群消息)"); pw.flush(); } } else { //单聊 for (int i = 1; i < valueAndUser.length; i++) { String username = valueAndUser[i]; User u = userMap.get(username); PrintWriter pw = new PrintWriter(u.getSocket().getOutputStream()); pw.println(user.getUsername() + ":" + valueAndUser[0] + "(来自私聊)"); pw.flush(); } } } } catch (IOException e) { System.out.println(user.getUsername()+"下线了"); } } }
package tcp_server; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; import java.util.HashMap; import java.util.Map; /** * @author 乔澳 * @version 1.0 * @title: Server * @projectName 20200222TCPServer * @description: * @date 2020/2/25 9:12 */ public class Server { static Map<String,User> userMap = new HashMap<String, User>(); public static void main(String[] args) { try { System.out.println("===server start==="); ServerSocket server = new ServerSocket(9999); while(true) { Socket socket = server.accept(); BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); String username = br.readLine(); User user = new User(username,socket); System.out.println(user.getUsername()+"上线啦"); userMap.put(username,user); ServerThread thread = new ServerThread(user); thread.start(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package tcp_server; import java.net.Socket; /** * @author 乔澳 * @version 1.0 * @title: User * @projectName 20200222TCPServer * @description: * @date 2020/2/27 8:23 */ public class User { public User(String username, Socket socket) { this.username = username; this.socket = socket; } private String username; private Socket socket; public User() { } public String getUsername() { return username; } public Socket getSocket() { return socket; } public void setUsername(String username) { this.username = username; } public void setSocket(Socket socket) { this.socket = socket; } }
客户端实现:
package tcp_client; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Socket; /** * @author 乔澳 * @version 1.0 * @title: ClientReader * @projectName 20200222TCPClient * @description: * @date 2020/2/26 16:11 */ public class ClientReader extends Thread { private Socket socket; public ClientReader(Socket socket){ this.socket = socket; } public void run(){ try { InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader reader = new BufferedReader(isr); for(;;){ String value = reader.readLine(); System.out.println(value); } } catch (IOException e) { System.out.println("对不起,服务器崩溃了"); } } }
package tcp_client; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; /** * @author 乔澳 * @version 1.0 * @title: ClientWriter * @projectName 20200222TCPClient * @description: * @date 2020/2/26 16:11 */ public class ClientWriter extends Thread{ private Socket socket; public ClientWriter(Socket socket){ this.socket = socket; } public void run(){ try { Scanner input = new Scanner(System.in); OutputStream os = socket.getOutputStream(); PrintWriter pw = new PrintWriter(os); while(true) { System.out.println("说点什么吧:"); String msg = input.nextLine(); pw.println(msg); pw.flush(); } } catch (IOException e) { e.printStackTrace(); } } }
package tcp_client; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.net.Socket; /** * @author 乔澳 * @version 1.0 * @title: QQFrame * @projectName 20200222TCPClient * @description: * @date 2020/2/28 9:46 */ public class QQFrame extends JFrame{ private String name; private JPanel panel = new JPanel(); private JTextArea mesgArea = new JTextArea(); private JScrollPane mesgPane = new JScrollPane(mesgArea); private JTextArea speakArea = new JTextArea(); private JScrollPane speakPane = new JScrollPane(speakArea); private JButton sendBtn = new JButton("发送"); private JButton resetBtn = new JButton("取消"); private Socket socket; public QQFrame(String name){ super(name); this.name = name; this.initOther(); this.initElement(); this.initSelf(); this.connectServer(); this.startReader(); this.initListener(); } private void connectServer(){ System.out.println("===client start==="); try { socket = new Socket("127.0.0.1",9999); PrintWriter pw = new PrintWriter(socket.getOutputStream()); pw.println(name); pw.flush(); } catch (IOException e) { JOptionPane.showMessageDialog(this,"服务器连接失败"); //e.printStackTrace(); } } private void startReader(){ //启动"读"线程 ClientReader reader = new ClientReader(); reader.start(); } private void initListener(){ ActionListener listener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { OutputStream os = socket.getOutputStream(); PrintWriter pw = new PrintWriter(os); String msg = speakArea.getText(); if(msg.isEmpty()) return; pw.println(msg); pw.flush(); speakArea.setText(""); } catch (IOException ee) { ee.printStackTrace(); } } }; sendBtn.addActionListener(listener); resetBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { speakArea.setText(""); } }); } private void initOther(){ panel.setLayout(null); mesgArea.setEditable(false); mesgPane.setBounds(10,10,305,220); speakPane.setBounds(10,240,305,140); sendBtn.setBounds(180,390,60,30); resetBtn.setBounds(260,390,60,30); } private void initElement(){ panel.add(mesgPane); panel.add(speakPane); panel.add(sendBtn); panel.add(resetBtn); this.add(panel); } private void initSelf(){ this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBounds(1400,220,350,500); this.setVisible(true); } public static void main(String[] args) { new QQFrame("乔乔"); new QQFrame("小红"); new QQFrame("小明"); } //内部类 public class ClientReader extends Thread { StringBuffer sb = new StringBuffer(); public void run(){ try { if(socket!=null) { InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader reader = new BufferedReader(isr); for (; ; ) { String value = reader.readLine(); sb.append(value + "\n"); mesgArea.setText(sb.toString()); } } } catch (IOException e) { e.printStackTrace(); } } } }
加载全部内容