import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException;
import util.*; /* * * @author zhang * 完成客户端请求处理 * */ public class ServerThread extends Thread{ private final int Error=0; //查询出错 private final int N_VALIDE=1;//不合法 private final int VALIDE=2;//合法 private Angent angent; private PrintWriter out; private String userId; private String password; private ChatProtocol cp; public ServerThread(Angent aangent) { angent=aangent; userId=angent.getUserId();//得到用户的帐号密码 password=angent.getPassword(); cp=new ChatProtocol(angent);//根据聊天内容建立一个protocol对象 }
public void run() { int i=isValid(); out.println(i);/*向客户端发送验证信息*/ if(i<VALIDE) this.stop(); //本线程中止 //登陆成功,进行消息处理和转接 cp.chat();//开始聊天 } private int isValid() {//验证用户的合法性 String sql="select from user where userId=? and password=?"; try { return SqlFactory.getPassSql().isValidate(sql,userId,password)?VALIDE:N_VALIDE; } catch (SQLException e) { System.out.println("用户验证查询时出错"); e.printStackTrace(); return Error; } } }

|