All of codes are from GeekAcademy.

Use ServerSocket to create a chat server - 1

MyServerSocket.java

public class MyServerSocket {

    public static void main(String[] args) {

        new ServerListener().start();
    }
}

ServerListener.java

public class ServerListener extends Thread {

    @Override
    public void run() {
                
        try {
            ServerSocket serverSocket = new ServerSocket(12345);        
            while(true){
                Socket socket = serverSocket.accept();
                JOptionPane.showMessageDialog(null, "A client connects to the local port 12345");        
                new ChatSocket(socket).start();
            }            
        } catch (IOException e) {
            e.printStackTrace();
        }    
    }
}

ChatSocket.java

public class ChatSocket extends Thread {

    Socket socket;
    public ChatSocket( Socket s) {
        this.socket = s;
    }
    
    public void out(String out) {
        try {
            socket.getOutputStream().write(out.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void run() {
        
        int count = 0;
        while(true){
            count++;
            out("loop: " + count + "  ");
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

 

Use ServerSocket to create a chat server - 2

ChatManager.java

public class ChatManager {

    private ChatManager() {
    }

    private static final ChatManager cm = new ChatManager();

    public static ChatManager getChatManager() {
        return cm;
    }

    Vector<ChatSocket> vector = new Vector<ChatSocket>();

    public void add(ChatSocket cs) {
        vector.add(cs);
    }

    public void publish(ChatSocket cs, String out) {
        for (int i = 0; i < vector.size(); i++) {
            ChatSocket vChatSocket = vector.get(i);
            if (!cs.equals(vChatSocket)) {
                vChatSocket.out(out);
            }
        }
    }
}

ServerListener.java

public class ServerListener extends Thread {

    @Override
    public void run() {

        try {
            ServerSocket serverSocket = new ServerSocket(12345);
            while (true) {
                Socket socket = serverSocket.accept();
                JOptionPane.showMessageDialog(null, "A client connects to the local port 12345");
                ChatSocket cs = new ChatSocket(socket);
                cs.start();
                ChatManager.getChatManager().add(cs);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

ChatSocket.java

public class ChatSocket extends Thread {

    Socket socket;

    public ChatSocket(Socket s) {
        this.socket = s;
    }

    public void out(String out) {
        try {
            socket.getOutputStream().write(out.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {

        try {
            BufferedReader bReader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
            String line = null;

            while ((line = bReader.readLine()) != null) {
                ChatManager.getChatManager().publish(this, line);
            }
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

 

 

 

 

 

                                                                                                                                                                                    J.X.Duasonir

 

posted on 2017-03-08 18:24  Mr.Winter  阅读(208)  评论(0编辑  收藏  举报