日期:2014-05-20 浏览次数:20818 次
package com.inscom.server.model;
import java.io.*;
import java.net.*;
import com.inscom.common.*;
import com.inscom.server.db.*;
import java.sql.*;
import java.util.*;
public class ChatThread extends Thread{
Socket s;
public ChatThread(Socket s){
this.s=s;
}
public void run(){
ObjectInputStream ois=null;
ObjectOutputStream oos=null;
try {
while(true){
//读取客户端发来的信息
ois=new ObjectInputStream(s.getInputStream());
Message m=(Message)ois.readObject();
if(m.getMessageType()==MessageType.comm_message){
//System.out.println(m.getSenderId()+" 发送给 "+m.getGetterId()+" 内容为: "+m.getInfo());
//完成转发
oos=new ObjectOutputStream(ManageChatThread.getChatThread(m.getGetterId()).s.getOutputStream());
oos.writeObject(m);
}else if(m.getMessageType()==MessageType.search_friends){
//到数据库中查找用户
if(m.getSearchFriendsMethod()==MessageType.search_friends_ic){
DBHelper helper=new DBHelper();
ResultSet rs=helper.Query("select * from UserInfo where id='"+m.getGetterId()+"'");
Vector<Object> vRows=null;
while(rs.next()){
//System.out.println(rs.getString("id")+" "+rs.getString("name")+" "+rs.getString("sex")+" "+rs.getString("birthdate"));
vRows=new Vector<Object>();
Vector<String> v=new Vector<String>();
v.add(rs.getString("id"));
v.add(rs.getString("name"));
v.add(rs.getString("sex"));
v.add(rs.getString("birthdate"));
vRows.add(v);
//System.out.println(v);
}
m.setOb(vRows);
oos=new ObjectOutputStream(s.getOutputStream());
oos.writeObject(m);
}
}
}
}catch(EOFException e2){
try {
if(s!=null){
s.close();
}
} catch (Exception e3) {
// TODO: handle exception
e3.printStackTrace();
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("异常");
e.printStackTrace();
}
}
}
服务启动,在8897端口监听....
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)
at java.io.ObjectOutputStream.writeNonProxyDesc(Unknown Source)
at java.io.ObjectOutputStream.writeClassDesc(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeFatalException(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at com.inscom.server.model.ChatThread.run(ChatThread.java:48)