One suggestion:
Please comment out the db access part under Parameters.LOAD_LIST and send some garbage data out. See what will happen. I am suspecting the DB connection might mess up the socket based on your description. It should not be that way. But...
Please comment out the db access part under Parameters.LOAD_LIST and send some garbage data out. See what will happen. I am suspecting the DB connection might mess up the socket based on your description. It should not be that way. But...
好像直到reply()这个方法里边调用到clientSocket.getOutputStream()之前,一切都正常啊。输出的数据库查询结果也是正常。但是因为clientSocket并没有作为参数传递给其他任何方法,所以也就不能在这个过程中知道clientSocket的连接状态。
难道要将clientSocket传进去测试吗?
还有你说的DB connection might mess up the socket这个情况到底是怎么样的呢?
能不能进一步解释一下?呵呵,我知道看代码是最头疼的啦。解决了另外开帖重谢啦。
{
private Socket socket;
private boolean isAlive = true; public Monitor(Socket socket)
{
this.socket = socket;
}
public void terminate()
{
isAlive = false;
}
public void run()
{
System.err.println("套接监视器线程开始");
if(socket != null)
{
int times = 0;
while(isAlive)
{
System.out.println("套接已经被关闭? -- " + socket.isClosed()
+ " -- 监视时间 -- " + times*20 + " 毫秒");
try{
Thread.currentThread().sleep(20);
}catch(Exception e){e.printStackTrace();}
times++;
}
System.err.println("套接监视器被终止");
}
else{
System.err.println("套接为空");
} System.err.println("套接监视器线程结束");
}
}
Server listening for incoming TCP connection on port 4225 ...TCP Connection received from: /127.0.0.1
8971231312500
1
Login Message Received
Login Authentication request from 1 with password 00650128
ResultSet successfully queried
PassCheck got : 00650128
Password the same: Check successful!
Process finished! Ready to send back reply
Socket is closed: false
OutputStream successfully got
TCP Connection received from: /127.0.0.1
Output sent
8971231312500
3
Search Message Received
Ready for new connections!
Process finished! Ready to send back reply
Socket is closed: false
OutputStream successfully got
Output sent
Ready for new connections!
TCP Connection received from: /127.0.0.1
8971231312500
7
Load_List Message Received
Ready to send the loaded list
Client Socket Status: false
套接监视器线程开始
套接已经被关闭? -- false -- 监视时间 -- 0 毫秒
套接已经被关闭? -- false -- 监视时间 -- 20 毫秒
套接已经被关闭? -- false -- 监视时间 -- 40 毫秒
LoadList()方法开始执行
LoadList()方法关闭输入流
LoadList()方法准备查询朋友ID
LoadList()方法查询朋友ID的SQL语句:
SELECT friend_id FROM JavaMe.FriendList_Table WHERE id = 1
LoadList()方法开始查询
套接已经被关闭? -- true -- 监视时间 -- 60 毫秒
套接已经被关闭? -- true -- 监视时间 -- 80 毫秒
套接已经被关闭? -- true -- 监视时间 -- 100 毫秒
套接已经被关闭? -- true -- 监视时间 -- 120 毫秒
套接已经被关闭? -- true -- 监视时间 -- 140 毫秒
LoadList()方法结束查询This user 1 has 1 friends in list
1 has friend : 1 -- ID# 2
After loading profile, we are sure the user 1 has 1 friends in list
Load List sql query for reference:
SELECT id, nickName FROM JavaMe.RegTable WHERE id = 2
LoadList()方法准备再次查询朋友信息
套接已经被关闭? -- true -- 监视时间 -- 160 毫秒
套接已经被关闭? -- true -- 监视时间 -- 180 毫秒
套接已经被关闭? -- true -- 监视时间 -- 200 毫秒
套接已经被关闭? -- true -- 监视时间 -- 220 毫秒
LoadList()方法查询完毕
Load List sql query for reference:
SELECT ip, status FROM JavaMe.Status_Table WHERE id = 2
LoadList()方法再次查询朋友IP
套接已经被关闭? -- true -- 监视时间 -- 240 毫秒
套接已经被关闭? -- true -- 监视时间 -- 260 毫秒
套接已经被关闭? -- true -- 监视时间 -- 280 毫秒
LoadList()方法查询完毕
127.0.0.1
套接已经被关闭? -- true -- 监视时间 -- 300 毫秒
1 Records of listed friends read
LoadList()方法共查询到记录数: 1
127.0.0.1
套接已经被关闭? -- true -- 监视时间 -- 320 毫秒
LoadList()方法输出流
Client Socket Status: true
Process finished! Ready to send back reply
套接监视器被终止
套接监视器线程结束
Socket is closed: true
IOException when sending replies
java.net.SocketException: Socket is closed
at java.net.Socket.getOutputStream(Unknown Source)
at ClientMessageHandler.reply(ClientMessageHandler.java:183)
at ClientMessageHandler.run(ClientMessageHandler.java:150)
Ready for new connections!
case Parameters.LOAD_LIST:
Monitor m = new Monitor(clientSocket); // 新建monitor
m.start(); //开始监视
System.err.println("Load_List Message Received");
dos.writeInt(Parameters.LOAD_LIST);
System.err.println("Ready to send the loaded list");
System.err.println("Client Socket Status: " + clientSocket.isClosed());
byte [] data2 = FriendList.loadList(dataInput).toByteArray();
System.err.println("Client Socket Status: " + clientSocket.isClosed());
dos.write(data2);
m.terminate(); //结束监视
break;
你在client中发完请求直接关闭连接,当然出错了!!!!
--------------------------------没有啊。
这样试试:
1. 在socket建好后,先不要建数据库连接,看一看此socket的两端端口.
2. 不建你的socket,只建数据库连接,看一看数据库的端口情况.
我的怀疑是数据库的socket把你的socket破坏了(不太可能?).
这个程序,到底是充当什么?监听(ServerSocket),还是请求(socket)用的?看了代码,有点糊涂啊!!!
如果有那位大哥对所给分数不满,先说抱歉啦。
可以到下面这个帖子去抢点分,哈哈:)
http://expert.csdn.net/Expert/topic/1756/1756989.xml?temp=.2653009