socket 그룹 채팅 실현 - 클라이언트마다 라인을 만듭니다.

클라이언트는 서버에서 보내는 메시지를 수신하고 서버에 메시지를 제출합니다
package socketmore;

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

/**
 *  
 */
class ReadMes extends Thread {
    private Socket socket;

    public ReadMes(Socket socket) {
        this.socket = socket;
    }

    public void run() {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(
                    socket.getInputStream()));
            String message = null;
            while (true) {
                message = reader.readLine();
                //  bye, 
                if (message.equals("bye")) {
                    break;
                }
                System.out.println(message);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

/**
 *
 *  
 */
class SendMes extends Thread {
    private Socket socket;

    public SendMes(Socket socket) {
        this.socket = socket;
    }

    public void run() {
        BufferedReader input = null;
        PrintWriter writer = null;
        try {
            input = new BufferedReader(new InputStreamReader(System.in));
            writer = new PrintWriter(socket.getOutputStream());
            String message = null;
            while (true) {
                message = input.readLine();
                //  bye 
                if (message.equals("bye")) {
                    break;
                }
                //  
                writer.println(message);
                writer.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (writer != null) {
                writer.close();
            }
        }
    }
}

public class ChatClient {
    private String ipAdress;

    public ChatClient(String ipAString) {
        this.ipAdress = ipAdress;
    }

    public void startWork() throws UnknownHostException, IOException {
        Socket socket = new Socket(ipAdress, 2345);
        new ReadMes(socket).start();
        new SendMes(socket).start();
    }

    /**
     * Description
     *
     * @param args
     * @throws UnknownHostException
     * @throws IOException
     */
    public static void main(String[] args) throws UnknownHostException,
            IOException {
        ChatClient chatClient = new ChatClient(args[0]);
        chatClient.startWork();
    }

}

서버 측에서 메시지를 수신하고 클라이언트마다 발송
package socketmore;

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.util.ArrayList;
import java.util.List;

/**
 *
 */
class Chat extends Thread {
    private Socket socket;
    private List<Socket> socketList;
    private int count;

    public Chat(int count, Socket socket, List<Socket> socketList) {
        this.count = count;
        this.socket = socket;
        this.socketList = socketList;
    }



    public void run() {
        BufferedReader reader = null;
        PrintWriter writer = null;
        try {
            reader = new BufferedReader(new InputStreamReader(
                    socket.getInputStream()));

            String message = null;
            while (true) {
                message = reader.readLine();
                //  bye , , bye 
                if (message.equals("bye")) {
                    writer = new PrintWriter(socket.getOutputStream());
                    writer.println("bye");
                    writer.flush();
                    continue;
                }

                //  , 
                for (int i = 0; i < socketList.size(); i++) {
                    writer = new PrintWriter(socketList.get(i)
                            .getOutputStream());
                    writer.println(count + " say: " + message);
                    writer.flush();
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public class ChatServer {

    public void startWork() throws IOException {
        ServerSocket serverSocket = new ServerSocket(2345);
        List<Socket> socketList = new ArrayList<Socket>();// 
        Socket socket = null;
        int count = 0;
        while (true) {
            socket = serverSocket.accept();
            count++;
            System.out.println(count + " clinet connected to the server!");
            //  , List 
            socketList.add(socket);
            //  , 
            new Chat(count, socket, socketList).start();
        }
    }

    public static void main(String[] args) throws IOException {
        ChatServer chatServer = new ChatServer();
        chatServer.startWork();
    }

}

좋은 웹페이지 즐겨찾기