美文网首页程序媛&程序猿云计算技术
Java实现简单的多人聊天程序

Java实现简单的多人聊天程序

作者: SophieSg | 来源:发表于2017-03-10 17:46 被阅读245次
    Server端代码实现

    Server端只实现简单的消息转发功能,为每一个客户端建立一个Thread,处理该客户端的消息转发。

    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.ArrayList;
    
    /**
     * Created by sandy on 10/03/2017.
     */
    public class MultiChatServer {
    
        ArrayList<ClientThread> clients = new ArrayList<ClientThread>();
    
        public static void main(String args []) {
    
            new MultiChatServer().start();
        }
    
        public void start() {
    
            try {
    
                boolean iConnect = false;
                ServerSocket serverSocket = new ServerSocket(20013);
                iConnect = true;
    
                while (iConnect) {
    
                    Socket socket = serverSocket.accept();
    
                    ClientThread currentClient = new ClientThread(socket);
                    clients.add(currentClient);
                    new Thread(currentClient).start();
                }
    
            } catch (IOException e) {
    
                e.printStackTrace();
    
            }
        }
    
        class ClientThread implements Runnable{
    
            private Socket socket;
            private DataInputStream dis;
            private DataOutputStream dos;
            private String str;
            private Boolean iConnect = false;
    
            ClientThread(Socket socket) {
                this.socket = socket;
                iConnect = true;
            }
    
            public void run() {
                try {
    
                    while (iConnect) {
    
                        dis = new DataInputStream(socket.getInputStream());
                        str = dis.readUTF();
                        int currentIndex = 0;
    
                        for (int i=0; i<clients.size(); i++) {
                            if (clients.get(i).equals(this)) {
                                currentIndex = i;
                                break;
                            }
                        }
    
                        for (int i = 0; i < clients.size(); i++) {
                            ClientThread clientThread = clients.get(i);
                            if (!clientThread.equals(this)) {
                                clientThread.sendMsg("Client-" + currentIndex + ": " + str);
                            }
    
                        }
                    }
                } catch (IOException e) {
    
                    e.printStackTrace();
                }
            }
    
            public void sendMsg(String str) {
    
                try {
    
                    dos = new DataOutputStream(this.socket.getOutputStream());
                    dos.writeUTF(str);
    
                } catch (IOException e) {
    
                    e.printStackTrace();
                }
            }
    
        }
    }
    
    
    
    Client端代码实现

    Client端需要启动两个线程,一个负责发送消息,一个负责接收消息,互不干扰。

    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.util.Scanner;
    
    /**
     * Created by sandy on 10/03/2017.
     */
    public class MultiChatClient {
    
        private Socket socket;
        private DataInputStream dis;
        private DataOutputStream dos;
        private Scanner scanner;
    
        public static void main(String args[]) {
    
            new MultiChatClient().start();
        }
    
        public void start() {
    
            try {
    
                socket = new Socket("localhost", 20013);
    
                dis = new DataInputStream(this.socket.getInputStream());
                dos = new DataOutputStream(this.socket.getOutputStream());
                scanner = new Scanner(System.in);
    
                new Thread(new SendThread()).start();
                new Thread(new RecThread()).start();
    
            } catch (Exception e) {
    
                e.printStackTrace();
            }
    
        }
    
        class SendThread implements Runnable {
    
            private String str;
            private Boolean iConnect = false;
    
    
            public void run() {
                iConnect = true;
                try {
                    while (iConnect) {
                        str = scanner.nextLine();
                        if (null == str || "".equals(str.trim()) || str.isEmpty() || str.length() == 0) {
                            continue;
                        } else {
                            dos.writeUTF(str);
                            System.out.println("I: " + str);
                        }
    
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        class RecThread implements Runnable {
    
            private String str;
            private Boolean iConnect = false;
    
            public void run() {
                iConnect = true;
                try {
                    while (iConnect) {
                        str = dis.readUTF();
                        System.out.println(str);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    

    相关文章

      网友评论

        本文标题:Java实现简单的多人聊天程序

        本文链接:https://www.haomeiwen.com/subject/qzlogttx.html