1. 程式人生 > >JAVA聊天室-極簡版

JAVA聊天室-極簡版

實現客戶端和服務端的連線並且可以群聊*##“`

客戶端

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner; public class Client { private Socket socket; public Client() { try { System.out.println("正在啟動客戶端"); socket = new Socket("localhost",8088); System.out.println("客戶端啟動成功"); } catch (Exception e) { } } public
void start() { try { OutputStream out = socket.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(out,"UTF-8"); PrintWriter pw = new PrintWriter(osw,true); Scanner scan = new Scanner(System.in); String name; while
(true) { System.out.println("輸入使用者名稱"); name =scan.nextLine(); if((name.length())>0) { break; }else { System.out.println("你輸入的使用者名稱不合法發"); } } System.out.println("歡迎"+name+"加入聊天室"); pw.println(name); ServerHandler handler =new ServerHandler(); Thread t = new Thread(handler); t.start(); String message = null; while(true) { pw.println(scan.nextLine()); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { try { Client c =new Client(); c.start(); }catch(Exception e) { System.out.println("啟動客戶端失敗"); } } //接收服務端發過來的訊息,並輸出 class ServerHandler implements Runnable { public void run(){ try { BufferedReader in =new BufferedReader(new InputStreamReader (socket.getInputStream(),"UTF-8")); String message =null; while((message=in.readLine())!=null) { System.out.println(message); } }catch(Exception e){ } } } }
服務端

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Server {

    private ServerSocket server;

    private List<PrintWriter> allOut;

    public Server() {
        try {
            server  = new ServerSocket(8088);

            allOut = new ArrayList<PrintWriter>();
        }catch(Exception e) {

        }
    }
    private synchronized void  addOut(PrintWriter out ){
        allOut.add(out);
    }
    private synchronized void removeOut(PrintWriter out){
        allOut.remove(out);
    }
    private synchronized void sendMessage(String message) {
        for(PrintWriter out : allOut ){
            out.println(message);
        }
    }
    public void start() {
        try {
            while(true) {
                System.out.println("等待客戶端連線");
                Socket socket = server.accept();
                System.out.println("客戶端已經連線");

                ClientHandler handler = new ClientHandler(socket);

                Thread t =new Thread(handler);
                t.start();
            }
        }catch(Exception e) {

        }
    }
    public static void main(String[] args) {
        try {
            Server s =new Server();
            s.start();
        }catch(Exception e){

        }


    }
    class ClientHandler implements Runnable{

        private Socket socket;
        private String host;
        private String nickName;

        public ClientHandler(Socket socket) {
            this.socket=socket;
            InetAddress address = socket.getInetAddress();
            host=address.getHostAddress();
        }

        public void run() {
            PrintWriter pw =null;
            try {

                BufferedReader br =new BufferedReader(new InputStreamReader (socket.getInputStream(),"UTF-8"));
                nickName =br.readLine();
                System.out.println(nickName+"上線了"+"他的IP地址為"+host);

                pw = new PrintWriter( new OutputStreamWriter( socket.getOutputStream(),"UTF-8"),true);
                addOut(pw);

                String message =null;

                while((message =br.readLine())!=null) {
                    sendMessage(nickName+"說"+message);
                }
            }catch(Exception e) {

            }finally {
                System.out.println(nickName+"下線了");
                removeOut(pw);
                try {
                    socket.close();
                }catch(IOException e) {
                    e.printStackTrace();
                }
            }





        }
    }

}