求援 Socket编程遇到的问题
想用Java的Socket做一个局域网的在线聊天系统 连接一个客户端的时候没问题 到连接第二个的时候就做死的报
java.net.SocketException: Connection reset 异常。 纠结。 恳请各位大牛支援:
这是客户端读的代码:
[code=Java][/code]package com.newer.client;
import
java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.List;
public class ClientReader extends Thread{
private Socket sk = null;
private ObjectInputStream ois = null;
private ClientJframe client=null;
private User user = null;
public ClientReader(Socket sk, ClientJframe client){
this.sk = sk;
this.client = client;
try {
ois = new ObjectInputStream(sk.getInputStream());
} catch (
IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run(){
while(true){
try {
Object obj = ois.readObject();
if(obj==null){
continue;
}
if(obj instanceof User){
client.display(user);
continue;
}else{
List list = (List)obj;
this.client.addList(list);
continue;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (
ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
这是客户端写的代码:
[code=Java][/code]package com.newer.client;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
public class ClientWriter extends Thread {
private Socket sk = null;
private ObjectOutputStream oos = null;
private boolean flag = false;
private User user = null;
public ClientWriter(User user, Socket sk) {
this.user = user;
this.sk = sk;
}
public Socket getSk() {
return sk;
}
public void setSk(Socket sk) {
this.sk = sk;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag, User user) {
this.flag = flag;
this.user = user;
this.notify();
}
public void run() {
try {
oos = new ObjectOutputStream(sk.getOutputStream());
oos.writeObject(user);
oos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是服务器的代码:
[code=Java][/code]package com.newer.server;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.newer.client.User;
public class ServerThread extends Thread {
private Socket sk = null;
private static List list = new ArrayList();
private static List listThread = Server.list;
private ObjectInputStream ois = null;
private ObjectOutputStream oos = null;
private User user = null;
public ServerThread(Socket sk) {
this.sk = sk;
init();
}
public void init() {
try {
ois = new ObjectInputStream(sk.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
while (true) {
try {
Object obj = ois.readObject();
if(obj==null){
continue;
}
user = (User) obj;
if (user.getuName() != null && !"".equals(user.getuName())) {