• 聊天室实现-客户端实现


    public class ClientSide {
        //服务端IP
        private String serverIP;
        //端口号
        private int port;
    
        public ClientSide(String serverIP,int port){
            this.serverIP=serverIP;
            this.port=port;
        }
    
        public void initClient(){
           try{
               //创建socket,连接服务端
               Socket client=new Socket(serverIP,port);
               //新建读取信息缓存区
               BufferedReader bufferedReader=null;
               //新建写入字符流
               PrintWriter printWriter=null;
               //控制台输入
                Scanner scanner = new Scanner(System.in);
    
               //获得输入流,将数据载入缓存区
               bufferedReader=new BufferedReader(new InputStreamReader(client.getInputStream()));
               //输出流,true为开启自动释放flush
               printWriter=new PrintWriter(client.getOutputStream(),true);
               //接收欢迎词
               System.out.println(readMessage(bufferedReader));
               String flag="";
               while (!flag.equals(Config.LoginSuccessCode)){
                   //接收'输入用户名'
                   System.out.println(readMessage(bufferedReader));
                   //控制台输入
                   String username = scanner.nextLine();
                   sentMessage(printWriter,username);
                   //接收'输入密码'
                   System.out.println(readMessage(bufferedReader));
                   //控制台输入
                   String psw = scanner.nextLine();
                   sentMessage(printWriter,psw);
                   //得到登录结果
                   flag=readMessage(bufferedReader);
                   if(Config.LoginErrCode.equals(flag)){
                       System.out.println("---用户名密码不匹配,请重新登录!---");
                   }
               }
               System.out.println("---欢迎使用聊天室,祝你聊天愉快---");
               //登录成功,开始聊天
               if(flag.equals(Config.LoginSuccessCode)){
                   //读取信息线程
                   new ReadClientThread(client.getInputStream()).start();
                   //输入信息线程
                   new SentClientThread(printWriter).start();
    
               }else{
                   System.out.println("登录异常!");
               }
    
    
           }catch (Exception e){
               System.out.println("发生异常,请联系管理员!");
           }
        }
    
        /**
         * 读取信息
         * @param bufferedReader
         */
        public String readMessage(BufferedReader bufferedReader) {
            String clinetMessage=null;
            try {
                clinetMessage= bufferedReader.readLine();
               // System.out.println("服务端:"+clinetMessage);
            }catch (Exception e){
                System.out.println("发生异常,请联系管理员!");
                //e.printStackTrace();
            }
            return clinetMessage;
        }
    
        /**
         * 发送信息
         * @param printWriter
         * @param message
         */
        public void sentMessage(PrintWriter printWriter,String message){
            printWriter.println(message);
            printWriter.flush();
        }
    
        public static void main(String[] args){
            ClientSide clientSide=new ClientSide("127.0.0.1",5000);
            clientSide.initClient();
        }
    
    }
    

      参考链接:https://blog.csdn.net/BlueSky_USC/article/details/54414860

  • 相关阅读:
    【LeetCode】Binary Tree Upside Down
    【LeetCode】171. Excel Sheet Column Number
    【LeetCode】Longest Substring with At Most Two Distinct Characters (2 solutions)
    【Algorithm】回溯法与深度优先遍历的异同
    【C++】自定义比较函数小结
    【LeetCode】4. Median of Two Sorted Arrays (2 solutions)
    【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)
    【LeetCode】Add Two Numbers
    【LeetCode】5. Longest Palindromic Substring
    【LeetCode】6. ZigZag Conversion
  • 原文地址:https://www.cnblogs.com/czgxxwz/p/10290902.html
Copyright © 2020-2023  润新知