聊天室实现-客户端实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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

posted @   唐宋丶元明清  阅读(154)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示