• Socket---聊天室


    ChatServer.java

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.ArrayList;
    
    public class ChatServer {
    
        private ArrayList<TongXinThread> list = new ArrayList<>();
    
        public void start(){
            //启动服务线程,循环accept
            new Thread(){
                public void run() {
                    try {
                        ServerSocket ss = new ServerSocket(8000);
                        System.out.println("服务已启动!");
                        while(true){
                            Socket s = ss.accept();
                            TongXinThread t = new TongXinThread(s);
                            t.start();
                            synchronized (list) {
                                list.add(t);    
                            }
                        }
                    } catch (Exception e) {
                        System.out.println("无法启动服务,或服务已停止!");
                    }
                };
            }.start();
        }
    
        class TongXinThread extends Thread{
            Socket s;
            BufferedReader in;
            PrintWriter out;
            public TongXinThread(Socket s) {
                this.s = s;
            }
            //发给当前客户端
            public void send(String msg){
                if(out != null){
                    out.println(msg);
                    out.flush();
                }
            }
            //发给所有客户端
            public void sendAll(String msg){
                synchronized (list) {
                    for (TongXinThread t : list) {
                        t.send(msg);
                    }    
                }
            }
            @Override
            public void run() {
                String name = null;
                try {
                    in = new BufferedReader(new InputStreamReader(s.getInputStream(),"GBK"));
                    out = new PrintWriter(new OutputStreamWriter(s.getOutputStream(),"GBK"));
                    //先接收昵称
                    name = in.readLine();
                    //给当前客户端发送欢迎信息
                    send(name+"欢迎进入激情聊天室!!");
                    //群发一个上线消息
                    synchronized (list) {
                        sendAll(name+"老司机进入了聊天室,在线老司机:"+list.size());
                    }
                    //循环从客户端接收内容,群发;
                    String line;
                    while((line = in.readLine()) != null){
                        sendAll(name+"说:"+line);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                synchronized (list) {
                    //当前客户端断开,把当前线程对象移除,this指自己
                    list.remove(this);
                    //群发一个下线消息
                    sendAll(name+"老司机离开了聊天室,在线老司机:"+list.size());
                }
                
            }
        }
        
        public static void main(String[] args) {
            ChatServer server = new ChatServer();
            server.start();
        }
        
    }

    ChatClient.java

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.util.LinkedList;
    import java.util.Scanner;
    
    public class ChatClient {
        BufferedReader in;
        PrintWriter out;
        private LinkedList<String> list = new LinkedList<>();
        private boolean flag = false;    //用户正在输入的标记
        public void start(){
            try {
                //连接服务器
                Socket s = new Socket("10.8.21.228",8000);
                in = new BufferedReader(new InputStreamReader(s.getInputStream(),"GBK"));
                out = new PrintWriter(new OutputStreamWriter(s.getOutputStream(),"GBK"));
                System.out.println("您的昵称:");
                String name = new Scanner(System.in).nextLine();
                out.println(name);
                out.flush();
                //接收服务器的聊天内容
                new Thread(){
                    public void run() {
                        receive();
                    };
                }.start();
                //让用户输入聊天内容,并发送
                new Thread(){
                    public void run() {
                        input();
                    };
                }.start();
                //打印线程
                new Thread(){
                    public void run() {
                        print();
                    };
                }.start();
            } catch (Exception e) {
                System.out.println("无法连接服务器,或连接被断开!");
            }
        }
        protected void print() {
            //从集合取出数据,从头部移除数据打印
            while(true){
                synchronized (list) {
                    while(flag == true || list.size() == 0){
                        try {
                            list.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    String s = list.removeFirst();
                    System.out.println(s);
                }
            }
        }
        protected void receive() {
            try {
                String line;
                while((line = in.readLine()) != null){
    //                System.out.println(line);
                    synchronized (list) {
                        list.add(line);
                        list.notifyAll();    //在集合上发通知
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("已经断开连接!");
        }
        protected void input() {
            System.out.println("嗯回车开始输入聊天内容:");
            while(true){
                new Scanner(System.in).nextLine();
                flag = true;    //正在输入
                System.out.print("输入聊天内容:");
                String s = new Scanner(System.in).nextLine();
                out.println(s);
                out.flush();
                flag = false;
                //通知正在等待的打印线程
                synchronized (list) {
                    list.notifyAll();
                }
            }
        }
        
        public static void main(String[] args) {
            ChatClient c = new ChatClient();
            c.start();
        }
    }
  • 相关阅读:
    第四次实验报告
    第三次实验报告
    第五章 循环结构课后反思
    第二次实验报告
    5-508寝室第六小组课后习题作业
    第一次实验报告
    第九章 构造数据类型实验
    第八章 指针实验
    第七章 数组实验
    第六章 函数和宏定义实验(2)
  • 原文地址:https://www.cnblogs.com/yikuan-919/p/9505772.html
Copyright © 2020-2023  润新知