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消息
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消息
解决方案 »
- 什么是字段的派生?
- enum中的数据如何赋初值?
- 请问这个程序中的super.fun()中的super是什么?
- 正则表达式请教!
- 关于字符串分割的问题,请大家帮忙想想
- 用dom对XML树进行遍历,如何思路啊!写出匡架代码
- 新手,if问题
- 如果将形如(MM/dd/yyyy HH:mm:ss.SSS)的字符串日期转成Date类型
- 请问jclass.table3.*所需的类库在JBuilder5中是哪个??在JBuilder3中是用Jbuilder3\lib\JCtable.jar
- 关于Java原子类的问题-初学者向
- (很困惑)一个关于 继承中使用super关键字调用父类的方法问题!
- java实现英语数字翻译阿拉伯数字,求大神
//保存本进程的客户计数
private static int cNumber= 0;
//保存与本线程相关的Socket对象
private Socket socket = null;
是这2个变量吗?