用 java 怎么写 multi-threaded client server chat?

问答 听说 ⋅ 于 2019-10-29 17:47:35 ⋅ 最后回复由 青牛 2019-10-30 08:35:58 ⋅ 68 阅读

用java 怎么写 multi-threaded client server chat?

成为第一个点赞的人吧 :bowtie:
回复数量: 1
  • 青牛 国内首批大数据从业者,就职于金山,担任大数据团队核心研发工程师
    2019-10-30 08:35:58

    public class server implements Runnable {// 服务端
    static List socketList=new ArrayList();
    // 读取 In
    static Socket socket = null;
    static ServerSocket serverSocket = null;
    public server() {// 构造方法
    try {
    serverSocket = new ServerSocket(9999);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("****服务端*****");
    server t = new server();
    int count = 0;
    while (true) {
    try {
    // System.out.println("端口9999等待被连接......");
    socket = serverSocket.accept();
    count++;
    System.out.println("第" + count + "个客户已连接");
    socketList.add(socket);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    Print p = new Print(socket);
    Thread read = new Thread(t);
    Thread print = new Thread(p);
    read.start();
    print.start();
    }
    }
    @Override
    public void run() {
    // 重写run方法
    try {
    Thread.sleep(1000);
    BufferedReader in = new BufferedReader(new InputStreamReader(socket
    .getInputStream()));
    while (true) {
    String jieshou = in.readLine();
    System.out.println( jieshou);
    for (int i = 0; i < socketList.size(); i++) {
    Socket socket=socketList.get(i);
    PrintWriter out = new PrintWriter(socket.getOutputStream());
    if (socket!=this.socket) {
    out.println(jieshou);
    }else{
    out.println("(你)"+jieshou);
    }
    out.flush();
    }
    }
    } catch (Exception e) {

            e.printStackTrace();
        }
    }

    }
    class Print implements Runnable {
    static List socketList=new ArrayList();
    Scanner input = new Scanner(System.in);
    public Print(Socket s) {// 构造方法
    try {
    socketList.add(s);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    @Override
    public void run() {
    try {
    Thread.sleep(1000);
    while (true) {
    String msg = input.next();
    for (int i = 0; i < socketList.size(); i++) {
    Socket socket=socketList.get(i);
    PrintWriter out = new PrintWriter(socket.getOutputStream());
    // System.out.println("对客户端说:");
    out.println("服务端说:"+msg);
    out.flush();
    }
    }
    } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    }
    }
    }
    //client.java
    package Socket;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.util.Scanner;
    public class client implements Runnable {// 客户端
    static Socket socket = null;
    Scanner input = new Scanner(System.in);
    static String name=null;
    public static void main(String[] args) {
    int x=(int)(Math.random()*100);
    client.name="client"+x;
    System.out.println("****客户端"+x+"*****");
    try {
    socket = new Socket("127.0.0.1", 9999);
    System.out.println("已经连上服务器了");
    } catch (Exception e) {
    e.printStackTrace();
    }
    client t = new client();
    Read r = new Read(socket);
    Thread print = new Thread(t);
    Thread read = new Thread(r);
    print.start();
    read.start();
    }
    @Override
    public void run() {
    try {
    Thread.sleep(1000);
    PrintWriter out = new PrintWriter(socket.getOutputStream());
    while (true) {
    String msg = input.next();
    out.println(name+"说:"+msg);
    out.flush();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    class Read implements Runnable {
    static Socket socket = null;
    public Read(Socket socket) {
    this.socket = socket;
    }
    @Override
    public void run() {
    try {
    Thread.sleep(1000);
    BufferedReader in = new BufferedReader(new InputStreamReader(socket
    .getInputStream()));
    while (true) {
    System.out.println( in.readLine());
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    ————————————————
    原文链接:https://blog.csdn.net/qq_29606255/article/details/78679815

暂无评论~~
  • 请注意单词拼写,以及中英文排版,参考此页
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
  • 支持表情,可用Emoji的自动补全, 在输入的时候只需要 ":" 就可以自动提示了 :metal: :point_right: 表情列表 :star: :sparkles:
  • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif,教程
  • 发布框支持本地存储功能,会在内容变更时保存,「提交」按钮点击时清空
Ctrl+Enter