• ServerSocket


    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

  • 相关阅读:
    Mysql(二)
    Mysql(一)
    JS图表插件(柱形图、饼状图、折线图)
    如何让Table中的第一列和第二列的值相乘然后赋值给第三列
    js生成验证码并验证
    IIS配置默认文档
    DropDownList如何添加一个空白的选项
    GridView如何合并同类项
    .NET后台如何获取前台HMTL控件的值
    Json数据报错
  • 原文地址:https://www.cnblogs.com/duasonir/p/6521993.html
Copyright © 2020-2023  润新知