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

类别:Java 点击:0 评论:0 推荐:
/*
 * 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;
}
}

本文地址:http://com.8s8s.com/it/it12479.htm