Java实现简单的聊天程序

作者: SophieSg | 来源:发表于2017-03-10 16:46 被阅读1030次

    练习利用Socket实现双人聊天的简单程序。

    Server端代码实现
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;
    
    /**
     * Created by sandy on 08/03/2017.
     */
    public class ChatServer {
    
        public  int port = 20013;
    
        public ChatServer() {
    
        }
    
        public ChatServer(int port) {
            this.port = port;
        }
    
        public void service() {
            try {
                ServerSocket server = new ServerSocket(port);
    
                Socket socket = server.accept();
    
                try {
                    DataInputStream in = new DataInputStream(socket.getInputStream());
    
                    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    
                    Scanner scanner = new Scanner(System.in);
    
                    while (true) {
                        String accept = in.readUTF();
                        System.out.println(accept);
    
                        String send = scanner.nextLine();
                        System.out.println("Server: " + send);
                        out.writeUTF("Server: " + send);
                    }
                } finally {
                    socket.close();
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static void main (String args []) {
            new ChatServer().service();
    
        }
    }
    
    
    Client端代码实现
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.util.Scanner;
    
    public class ChatClient {
        private String host = "localhost";
        private int port = 20013;
    
        public ChatClient() {
        
        }
    
        public ChatClient(String host, int port) {
            this.host = host;
            this.port = port;
        }
    
        public void chat() {
            try {
                Socket socket = new Socket(host, port);
    
                try {
                    DataInputStream in = new DataInputStream(socket.getInputStream());
    
                    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    
                    Scanner scanner = new Scanner(System.in);
    
                    while (true) {
                        String send = scanner.nextLine();
                        System.out.println("Client: " + send);
                        out.writeUTF("Client: " + send);
    
                        String accept = in.readUTF();
                        System.out.println(accept);
                    }
                } finally {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();    
            }
        }
    
        public static void main(String args []) {
            new ChatClient().chat();
        }
    }
    
    

    相关文章

      网友评论

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

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