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();
}
}