客户端

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

public class client {
    Socket socket;
    PrintWriter pw;
    BufferedReader br;
    public client() throws UnknownHostException {
        try {
            socket = new Socket("127.0.0.1",28889);
            //写 发送
            pw = new PrintWriter(socket.getOutputStream());
            pw.println("我是张三");
            pw.flush();
            //读 接收
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            new GetMessageServer().start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    class GetMessageServer extends Thread{
        public void run() {
            while(this.isAlive()) {
                try {
                    System.out.println(br.readLine());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    System.out.println("服务端未启动");
                    break;
                }
                
            }
        }
    }
    public static void main(String args[]) {
        try {
            new client();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

服务端

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;

public class server {
    ServerSocket serverSocket;
    ArrayList<BufferedReader> brs = new ArrayList<BufferedReader>();
    ArrayList<PrintWriter> pws = new ArrayList<PrintWriter>();
    LinkedList<String> msgs = new LinkedList<String>();
    private int num = 0;
    public server() {
        try {
            serverSocket = new ServerSocket(28889);
            new AcceptSocketThread().start();
            new SendMsgThread().start();
            System.out.println("服务端已启动.....");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    class AcceptSocketThread extends Thread{
         public void run() {
             while(this.isAlive()) {
                 try {
                    Socket socket = serverSocket.accept();
                    if(socket != null) {
                        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        brs.add(br);
                        num++;
                        pws.add(new PrintWriter(socket.getOutputStream()));
                        new GetMsgThread(br).start();                    
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
             }
         }
    }
    class GetMsgThread extends Thread{
        BufferedReader br;
        public GetMsgThread(BufferedReader br) {
            this.br = br;
        }
        public void run() {
            while(this.isAlive()) {
                String msg;
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    msg = br.readLine();
                    if(msg != null) {
                        System.out.println(num+"  "+msg.substring(2));
                        String strTime = dateFormat.format(new Date());
                        msgs.addFirst("<== " + strTime + " 当前客户端数量为 "+ num +"==>\n" + msg.substring(2));
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    String strTime = dateFormat.format(new Date());
                    msgs.addFirst("<== " + strTime + " 有客户端下线了。 ==>\\n");
                    num--;
                    System.out.println("有客户端下线了。");
                    break;
                }

            }
        }
    }
    class SendMsgThread extends Thread {
        public void run() {
            while(this.isAlive()) {
                try {
                    if(!msgs.isEmpty()) {
                        String msg = msgs.removeLast();
                        for(int i=0;i<pws.size();i++) {
                            pws.get(i).println(msg);
                            pws.get(i).flush();
                        }
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new server();
    }

}
最后修改:2019 年 12 月 06 日 12 : 14 AM