• 实现多人聊天


    服务器端代码

    package Zn200611wzyfb;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    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.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Scanner;

    public class ServerThread extends Thread{
    static final Map <String,Socket> mp = new HashMap<String,Socket>();

    static final List<Socket> li = new ArrayList<Socket>();
    OutputStream output;//输出流
    InputStream input;//输入流
    Socket socket;//定义一个socket接收对象的属性
    //线程创建方法


    public void addUser(Socket ss) {
    li.add(ss);
    }

    Scanner sca = new Scanner(System.in);
    public synchronized void run() {
    try {
    System.out.println("已启动一个线程来处理~");

    System.out.println("增加用户成功");

    System.out.println("当前用户"+li.size());




    //为输入输出流赋值
    output=socket.getOutputStream();
    input=socket.getInputStream();



    PrintWriter pw = new PrintWriter(socket.getOutputStream());
    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    //接收
    Thread t1 = new Thread(new Runnable() {
    PrintWriter pw;
    @Override
    public synchronized void run() {
    while(true){
    try {
    String st = br.readLine();
    System.out.println(st); //这里不会造成死循环,这个readline会等待接受


    for (int i = 0; i < li.size(); i++) {

    if(socket.equals(li.get(i))) {
    li.remove(i);
    }

    }
    for (Socket socket : li) {

    // System.out.println("当前用户为"+socket);
    PrintWriter pw = new PrintWriter(socket.getOutputStream());
    //stt = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    pw.println(st);
    pw.flush();
    }

    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    }
    });

    t1.start();


    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    //定义一个建立服务器的方法

    private void setUpServer(int port) throws IOException {
    //将输入的端口设置为服务器
    ServerSocket server=new ServerSocket(port);
    //输出当前服务器的端口号
    System.out.println("服务器创建成功,端口号:"+server.getLocalPort());

    while(true) {
    ServerThread ts=new ServerThread();
    //为对象的socket属性赋值。 server.accept(); 进入阻塞状态,可以在控制台写入
    Socket sk = server.accept();
    // mp.put("eee",sk);

    addUser(sk);

    ts.socket= sk;

    //启动线程
    ts.start();

    }

    }


    //主函数入口
    public static void main(String[] args) throws IOException {

    //创建一个通信类的对象
    ServerThread server=new ServerThread();

    server.setUpServer(9013);

    }
    }

    客户端代码(每执行一下客户端,增加一个人聊天)

    package Zn200611wzyfb;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.Scanner;

    public class Clils {

    public static void main(String[] args) throws Exception {

    //张三客户端
    Scanner sca = new Scanner(System.in);
    System.out.println("----------这里是客户端,请输入用户名------------");

    String name = sca.next();

    System.out.println("欢迎"+name);

    Socket sc = new Socket("127.0.0.1",9013);

    PrintWriter pw = new PrintWriter(sc.getOutputStream());
    BufferedReader br = new BufferedReader(new InputStreamReader(sc.getInputStream()));

    //接收
    Thread t1 = new Thread(new Runnable() {
    @Override
    public void run() {
    while(true){
    try {
    System.out.println(br.readLine()); //这里不会造成死循环,这个readline会等待接受
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    }
    });

    t1.start();

    //写

    Thread t2 = new Thread(new Runnable() {

    Scanner sca = new Scanner(System.in);
    @Override
    public void run() {
    while(true) {
    String speak = Thread.currentThread().getName();
    pw.println(speak+"说:"+sca.next());
    pw.flush();
    }

    }
    });
    t2.setName(name);
    t2.start();
    }

    }

  • 相关阅读:
    python 替换图片中的像素
    python paddleocr 增加识别速度的方法
    mysql 子查询少用
    mysql 临时表代替in的方法
    mysql创建临时表不用创建临时表结构的方法
    Nginx 报错 connect() failed (110: Connection timed out) while connecting to upstream
    用mkcert生成SSL,内网完美起跑https
    用于制作任何echarts地图(省、市、区、城镇街道)
    javascript执行机制
    ES6便捷语法
  • 原文地址:https://www.cnblogs.com/1306962984wei/p/13097302.html
Copyright © 2020-2023  润新知