记录socket问题
private void initSocket(){
InetAddress addr = null;
try {
addr = InetAddress.getLocalHost();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
log.info("ip = " + addr.getHostAddress().toString());
linkList = new LinkedList();
new Thread(new Runnable() {
@Override
public void run() {
try {
// 统计客户端总数
int clientCount = 0;
//是否对客户端进行监听
boolean listening = true;
int fileprot;
try{
fileprot = Integer.parseInt(Util.getFileIO("prot"));
}catch(Exception ex){
log.error("读取配置端口配置文件错误");
fileprot = port;
}
try {
//创建端口4899的监听对象,监听客户请求
server = new ServerSocket(fileprot);
} catch (Exception e) {
// System.out.println("Can not listen to. " + e);
log.error("不能监听此端口" + e);
}
while (listening) {
//客户端计数
clientCount++;
//监听到客户请求,根据得到的Socket对象和客户计数创建服务线程,并启动
new MyServerThread(server.accept(), clientCount)
.start();
}
} catch (Exception e) {
// System.out.println("Can not listen to. " + e);
log.error("Error. " + e);
}
}
}).start();
new Thread(new QueseThread()).start();
}
//上为启动socket服务并监听客户端请求。
public MyServerThread(Socket socket, int clientCount) {
this.socket = socket;
cNumber = clientCount;
// System.out.println("当前在线的用户数: " + cNumber);
MainFrame.log.info("当前用户编号: " + cNumber);
}
@SuppressWarnings("unchecked")
public void run() {
try {
String line;
BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter os = new PrintWriter(socket.getOutputStream());
line = is.readLine();
String[] e = line.split(",");
while(!line.equals("close")){
MainFrame.log.info("用户第" + cNumber + "号,发送来的命令" + line);
MainFrame.log.info("当前用户socket: " + socket);
//此处的socket对象和cNumber已经出现重复。
如何去除这种重复对象,因为还要返回给client消息