封装了客户socket和客户的userId的代理类----Agent

1/5/2008来源:Java教程人气:6256

/*
 * Created on 2004-12-15
 *  //该类解析了socket的用户名 userId,passWord,等待Thread验证
 *  封装了客户socket和客户的userId
 * ???根据专家模式有两种方式:1 让thread处理
 *                       2 让Agent自己处理.在Thread处调用即可
 *                       这两种方法哪个更好一些??
 */
 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PRintWriter;
import java.net.*;
import java.util.StringTokenizer;
public class Angent {
   private  Socket socket;
   public String msg;//在这里解析,线程类,协议类可以利用
   private  String userId;
   private  String password;
   private BufferedReader in;
   private  PrintWriter out;
 
public String getPassword() {
 return password;
}
 
public Socket getSocket() {
 return socket;
}
 
public String getUserId() {
 return userId;
}
public Angent(Socket asocket) {
 socket=asocket;
 try {
   in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
   out= new PrintWriter(socket.getOutputStream(),true/*autoflush*/);
   msg=in.readLine();
  getuser();//初始化字段
 } catch (IOException e) {
  System.out.println("解析字符错误!");
  e.printStackTrace();
 }
 }
//解析命令,检测合法用户(用户首次登录)
private void getuser() throws IOException{
 StringTokenizer t=new StringTokenizer(msg,"//");
    userId=t.nextToken();
    password=t.nextToken();
 }
 
public BufferedReader getIn()  {
 return in;
}
 
public PrintWriter getOut() {
 return out;
}
}