• Java多人聊天室第一版


     1 package cn.zhang.chat;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.PrintWriter;
     5 
     6 /**
     7   * 所有用户均有的信息,单独设为一个类
     8  * @author 
     9  *
    10  */
    11 
    12 public class Account {
    13 
    14     //封装,属性设为私有
    15     private String name;
    16     private BufferedReader reader;
    17     private PrintWriter writer;
    18 
    19     //设置相应的set和get方法
    20     public String getName() {
    21         return name;
    22     }
    23 
    24     public void setName(String name) {
    25         this.name = name;
    26     }
    27 
    28     public BufferedReader getReader() {
    29         return reader;
    30     }
    31 
    32     public void setReader(BufferedReader reader) {
    33         this.reader = reader;
    34     }
    35 
    36     public PrintWriter getWriter() {
    37         return writer;
    38     }
    39 
    40     public void setWriter(PrintWriter writer) {
    41         this.writer = writer;
    42     }
    43 }
     1 package cn.zhang.chat;
     2 
     3 import java.io.*;
     4 import java.net.Socket;
     5 import java.util.Scanner;
     6 
     7 public class ChatClient {
     8 
     9     public static void main(String[] args) throws Exception {
    10         Scanner sc = new Scanner(System.in);
    11 
    12         while (true) {
    13             System.out.print("请输入昵称:");
    14             String name = sc.nextLine();
    15 
    16             Socket s = new Socket(UserData.HOST, UserData.PORT);
    17             InputStream in = s.getInputStream();
    18             OutputStream out = s.getOutputStream();
    19 
    20             BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    21             PrintWriter writer = new PrintWriter(new OutputStreamWriter(out), true);
    22 
    23             writer.println(name);
    24             String temp = reader.readLine();
    25             if (temp.equals("success")) {
    26                 // 启动新线程监听消息
    27                 Account user = new Account();
    28                 user.setReader(reader);
    29                 user.setWriter(writer);
    30                 user.setName(name);
    31                 ReceiveMessage rm = new ReceiveMessage(user);
    32                 new Thread(rm).start();
    33 
    34                 // 给服务器发消息
    35                 while (true) {
    36                     
    37                     String msg = sc.nextLine();
    38                     writer.println(msg);
    39                     
    40                 }
    41 
    42             } else {
    43                 
    44                 System.out.println(temp);
    45                 
    46             }
    47         }
    48     }
    49 }
     1 package cn.zhang.chat;
     2 
     3 import java.io.*;
     4 import java.net.ServerSocket;
     5 import java.net.Socket;
     6 /**
     7   * 服务器端的ChatServer
     8  * @author 
     9  *
    10  */
    11 public class ChatServer {
    12 
    13     public static void main(String[] args) throws Exception {
    14 
    15         //等待用户连接之前,创建一个SeverSocket
    16         ServerSocket ss = new ServerSocket(UserData.PORT);
    17 
    18         while (true) {
    19             System.out.println("等待链接");
    20             Socket s = ss.accept();
    21 
    22             InputStream in = s.getInputStream();
    23             OutputStream out = s.getOutputStream();
    24 
    25             BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    26             PrintWriter writer = new PrintWriter(new OutputStreamWriter(out), true);
    27 
    28             String name = reader.readLine();//读入名字
    29 
    30             boolean flag = false;
    31             
    32             //foreach循环遍历名字,若名字相同,则返回用户名已存在
    33             for (Account a : UserData.USERS) {
    34                 if (a.getName().equals(name)) {
    35                     flag = true;
    36                 }
    37             }
    38             if (flag) {
    39                 writer.println("用户名已存在~~~");
    40             } else {
    41                 writer.println("success");
    42             }
    43 
    44             // 启动一个新的线程监听消息
    45             Account user = new Account();
    46             user.setName(name);
    47             user.setWriter(writer);
    48             user.setReader(reader);
    49 
    50             SendMessage sm = new SendMessage(user);
    51             new Thread(sm).start();
    52 
    53             UserData.USERS.add(user);
    54             for (Account a : UserData.USERS) {
    55                 a.getWriter().println("[" + name + "]进入聊天室,当前在线人数:" + UserData.USERS.size());
    56             }
    57         }
    58 
    59     }
    60 
    61 }
     1 package cn.zhang.chat;
     2 
     3 public class ReceiveMessage implements Runnable {
     4 
     5     private Account user;
     6 
     7     public ReceiveMessage(Account user) {
     8         this.user = user;
     9     }
    10 
    11     @Override
    12     public void run() {
    13         while (true) {
    14             try {
    15                 String msg = this.user.getReader().readLine();
    16                 System.out.println(msg);
    17             } catch (Exception e) {
    18                 e.printStackTrace();
    19             }
    20         }
    21     }
    22 }
     1 package cn.zhang.chat;
     2 
     3 
     4 import java.io.IOException;
     5 
     6 public class SendMessage implements Runnable {
     7 
     8     private Account user;
     9 
    10     public SendMessage(Account user) {
    11         this.user = user;
    12     }
    13 
    14     //重写父类方法
    15     @Override
    16     public void run() {
    17         while (true) {
    18             try {
    19                 String msg = this.user.getReader().readLine();
    20                 for (Account a : UserData.USERS) {
    21                     if (!a.getName().equals(this.user.getName())) {
    22                         a.getWriter().println(this.user.getName() + " 说:" + msg);
    23                     }
    24                 }
    25 
    26             } catch (Exception e) {
    27                 UserData.USERS.remove(this.user);
    28                 for (Account a : UserData.USERS) {
    29                     System.out.println("[" + this.user.getName() + "]退出了聊天室,当前在线人数:" + UserData.USERS.size());
    30                     a.getWriter().println("[" + this.user.getName() + "]退出了聊天室,当前在线人数:" + UserData.USERS.size());
    31                     break;
    32                 }
    33                 
    34                 e.printStackTrace();
    35             }
    36         }
    37     }
    38 }
     1 package cn.zhang.chat;
     2 import java.util.ArrayList;
     3 import java.util.List;
     4 /**
     5   * 用户基本信息
     6  * @author 
     7  *
     8  */
     9 public class UserData {
    10 
    11     public static final String HOST = "127.0.0.1";
    12     public static final int PORT = 8888;
    13 
    14     public static final List<Account> USERS = new ArrayList<>();
    15 
    16 }
  • 相关阅读:
    字符串拼接练习
    java数字字符的全半角转化
    mysql 的group by 满足的规则要求:
    从指定字符串获取n个随机数
    jQuery去除空格的$.trim()
    mysql里的位运算小结
    sql的case 用法
    泛型之元组示例
    jQuery里的replaceAll和replaceWith
    根据索引条件查询结果与原表关联的查询
  • 原文地址:https://www.cnblogs.com/zhangqiling/p/11389355.html
Copyright © 2020-2023  润新知