聊天室-服务端线程处理客户端信息

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
public class ServerThread extends  Thread {
    //需要处理的socket
    private Socket socket;
    //该线程对应的用户信息
    private User user;
    //新建读取信息缓存区
    public BufferedReader bufferedReader;
    //新建写入字符流
    public PrintWriter printWriter;
 
    //通过构造方法,初始化socket
    public ServerThread(Socket socket){
        this.socket=socket;
    }
    //该线程运行内容
    public void run(){
 
        try{
            //获得输入流,将数据载入缓存区
            bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //输出流,true为开启自动释放flush
            printWriter=new PrintWriter(socket.getOutputStream(),true);
            //欢迎词
            sentMessage(printWriter,"欢迎登入聊天室"+",当前在线用户个数:"+ServerSide.clinets.size());
            System.out.println("-----------------进入用户登录阶段-----------------");
            //进行登录操作
            user=Login(bufferedReader,printWriter);
            //登录成功之后,再加入到线程列表中
            ServerSide.clinets.add(this);
            System.out.println("-----------------进入用户信息转发阶段-----------------");
            //登录完成后,进入该用户信息转发阶段
            forwardMessage(bufferedReader,user);
            System.out.println("-----------------进入用户退出阶段-----------------");
 
            //sentMessage(printWriter,"退出成功!");
            bufferedReader.close();
            printWriter.close();
            this.socket.close();
            //从列表删除该线程
            ServerSide.clinets.remove(this);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
 
    /**
     * 信息转发阶段
     * @param bufferedReader
     * @return
     */
    public void forwardMessage(BufferedReader bufferedReader,User user){
        String clinetMessage=readMessage(bufferedReader);
        //Config.ExitCode为退出指令,为‘886’
        while (!Config.ExitCode.equals(clinetMessage)){
            for(int i=0;i<ServerSide.clinets.size();i++){
                ServerThread serverThread=ServerSide.clinets.get(i);
                //自身不需要发送数据
                if(serverThread!=this){
                    //获得对应的printWriter,发送短信
                    sentMessage(serverThread.printWriter,user.getName()+":"+clinetMessage);
                }
            }
            //等待下条数据
            clinetMessage=readMessage(bufferedReader);
        }
    }
 
    /**
     * 初始化登录环节
     * @param bufferedReader
     * @param printWriter
     * @return
     */
    public User Login(BufferedReader bufferedReader,PrintWriter printWriter){
        User user=new User();
        Boolean flag=false;
        while (!flag){
            sentMessage(printWriter,"请输入初始用户名:");
            String name=readMessage(bufferedReader);
            user.setName(name);
            sentMessage(printWriter,"请输入初始密码:");
            String psw=readMessage(bufferedReader);
            user.setPsw(psw);
            //校验账号密码
            flag=checkUser(user);
            //登录失败,返回结果
            if(!flag){
                sentMessage(printWriter,Config.LoginErrCode);
            }
        }
        //登录成功后,设置身份标示
        user.setId(UUID.randomUUID().toString());
        sentMessage(printWriter,Config.LoginSuccessCode);
        System.out.println(user.toString()+",登录成功!"+",当前在线用户个数:"+ServerSide.clinets.size());
        return user;
    }
 
    /**
     * 检查用户是否正确
     * 这里固定了两个用户
     * @param user
     * @return
     */
    public Boolean checkUser(User user){
        if(("admin".equals(user.getName())&&"123456".equals(user.getPsw()))||
                ("root".equals(user.getName())&&"123456".equals(user.getPsw()))||
                     ("czg".equals(user.getName())&&"123456".equals(user.getPsw()))){
            return true;
        }
        return false;
    }
 
    /**
     * 读取信息
     * @param bufferedReader
     */
    public String readMessage(BufferedReader bufferedReader) {
        String clinetMessage="";
        try {
            clinetMessage= bufferedReader.readLine();
            String username=(user==null?"未登录用户":user.getName());
            System.out.println(username+":"+clinetMessage);
        }catch (Exception e){
            e.printStackTrace();
        }
        return clinetMessage;
    }
 
    /**
     * 发送信息
     * @param printWriter
     * @param message
     */
    public void sentMessage(PrintWriter printWriter,String message){
        printWriter.println(message);
        printWriter.flush();
    }
}

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

public class ServerThread extends  Thread {
//需要处理的socket
private Socket socket;
//该线程对应的用户信息
private User user;
//新建读取信息缓存区
public BufferedReader bufferedReader;
//新建写入字符流
public PrintWriter printWriter;

//通过构造方法,初始化socket
public ServerThread(Socket socket){
this.socket=socket;
}
//该线程运行内容
public void run(){

try{
//获得输入流,将数据载入缓存区
bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//输出流,true为开启自动释放flush
printWriter=new PrintWriter(socket.getOutputStream(),true);
//欢迎词
sentMessage(printWriter,"欢迎登入聊天室"+",当前在线用户个数:"+ServerSide.clinets.size());
System.out.println("-----------------进入用户登录阶段-----------------");
//进行登录操作
user=Login(bufferedReader,printWriter);
//登录成功之后,再加入到线程列表中
ServerSide.clinets.add(this);
System.out.println("-----------------进入用户信息转发阶段-----------------");
//登录完成后,进入该用户信息转发阶段
forwardMessage(bufferedReader,user);
System.out.println("-----------------进入用户退出阶段-----------------");

//sentMessage(printWriter,"退出成功!");
bufferedReader.close();
printWriter.close();
this.socket.close();
//从列表删除该线程
ServerSide.clinets.remove(this);
}catch (Exception e){
e.printStackTrace();
}
}


/**
* 信息转发阶段
* @param bufferedReader
* @return
*/
public void forwardMessage(BufferedReader bufferedReader,User user){
String clinetMessage=readMessage(bufferedReader);
//Config.ExitCode为退出指令,为‘886’
while (!Config.ExitCode.equals(clinetMessage)){
for(int i=0;i<ServerSide.clinets.size();i++){
ServerThread serverThread=ServerSide.clinets.get(i);
//自身不需要发送数据
if(serverThread!=this){
//获得对应的printWriter,发送短信
sentMessage(serverThread.printWriter,user.getName()+":"+clinetMessage);
}
}
//等待下条数据
clinetMessage=readMessage(bufferedReader);
}
}

/**
* 初始化登录环节
* @param bufferedReader
* @param printWriter
* @return
*/
public User Login(BufferedReader bufferedReader,PrintWriter printWriter){
User user=new User();
Boolean flag=false;
while (!flag){
sentMessage(printWriter,"请输入初始用户名:");
String name=readMessage(bufferedReader);
user.setName(name);
sentMessage(printWriter,"请输入初始密码:");
String psw=readMessage(bufferedReader);
user.setPsw(psw);
//校验账号密码
flag=checkUser(user);
//登录失败,返回结果
if(!flag){
sentMessage(printWriter,Config.LoginErrCode);
}
}
//登录成功后,设置身份标示
user.setId(UUID.randomUUID().toString());
sentMessage(printWriter,Config.LoginSuccessCode);
System.out.println(user.toString()+",登录成功!"+",当前在线用户个数:"+ServerSide.clinets.size());
return user;
}

/**
* 检查用户是否正确
* 这里固定了两个用户
* @param user
* @return
*/
public Boolean checkUser(User user){
if(("admin".equals(user.getName())&&"123456".equals(user.getPsw()))||
("root".equals(user.getName())&&"123456".equals(user.getPsw()))||
("czg".equals(user.getName())&&"123456".equals(user.getPsw()))){
return true;
}
return false;
}

/**
* 读取信息
* @param bufferedReader
*/
public String readMessage(BufferedReader bufferedReader) {
String clinetMessage="";
try {
clinetMessage= bufferedReader.readLine();
String username=(user==null?"未登录用户":user.getName());
System.out.println(username+""+clinetMessage);
}catch (Exception e){
e.printStackTrace();
}
return clinetMessage;
}

/**
* 发送信息
* @param printWriter
* @param message
*/
public void sentMessage(PrintWriter printWriter,String message){
printWriter.println(message);
printWriter.flush();
}
}
posted @   唐宋丶元明清  阅读(233)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示