SocketException:连接重置服务器

时间:2016-01-17 14:58:40

标签: java sockets server socketexception

我正在做一个与服务器相关的项目。这是服务器代码:

public class Server {

    public final int PORT = 8888;

    private ServerSocket server;
    private ArrayList<HandlerOut> arrayout= new ArrayList<HandlerOut>();
    private HashMap <String,ArrayList <Mensagem>> hashmensagens = new HashMap<String , ArrayList <Mensagem>>();

    public Server() {
        try {
            this.startServing();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void startServing() throws IOException {
        server = new ServerSocket(PORT);
        System.out.println("Lançou ServerSocket: " + server);
            while(true) {
                System.out.println("A aceitar...");
                Socket socket = server.accept();
                System.out.println(socket);
                ObjectOutputStream output = new ObjectOutputStream (socket.getOutputStream());
                output.flush();
                ObjectInputStream input = new ObjectInputStream (socket.getInputStream());
                HandlerIn hi = new HandlerIn(this, input, output);
                hi.start();
            }   
    }

    public ArrayList<HandlerOut> getArrayout() {
        return arrayout;
    }

    public HashMap<String, ArrayList<Mensagem>> getHashmensagens() {
        return hashmensagens;
    }

    public static void main(String[] args) {    
        Server s = new Server();
    }   

} 

这是HandlerIn代码:

public class HandlerIn extends Thread {    

    private ObjectInputStream input;
    private Server server;
    private ObjectOutputStream output;

    public HandlerIn(Server server, ObjectInputStream input, ObjectOutputStream output) {
        super();
        this.server = server;
        this.input = input;
        this.output = output;
    }    

    public void run(){
        while(true){
            Mensagem m = null;
            try {
                m = (Mensagem)input.readObject();
                System.out.println(m);
                if(m.getReceivers() == null){
                    HandlerOut ho = new HandlerOut(server, m.getAuthor(), output);
                    ho.start();
                    server.getArrayout().add(ho);
                }
                else{
                    for(String receiver :m.getReceivers()){
                        ArrayList<Mensagem> v = server.getHashmensagens().get(receiver);
                        v.add(m);
                        server.getHashmensagens().put(receiver,v);
                    }
                }
                ArrayList <Mensagem> am;
                if(server.getHashmensagens().get(m.getAuthor()) == null){
                    am = new ArrayList<Mensagem>();
                }
                else{
                    am = server.getHashmensagens().get(m.getAuthor());
                }
                am.add(m);
                server.getHashmensagens().put(m.getAuthor(),am);
                notificaThreadOut(m);
            } catch (ClassNotFoundException e) {    

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private synchronized void notificaThreadOut(Mensagem m) {
        if(m.getReceivers() != null){
            for(String receiver: m.getReceivers()){
                for(HandlerOut ho: server.getArrayout()){
                    if(receiver.equals(ho.getUsername())){
                        synchronized (ho) {
                            ho.notify();
                        }
                    }
                }
            }
        }
    }
}

这是我的HandlerOut代码:

public class HandlerOut extends Thread {
    private Server server;
    private String username;
    private ObjectOutputStream output;

    public HandlerOut(Server server, String username, ObjectOutputStream output){
        this.server = server;
        this.username = username;
        this.output = output;
    }

    public String getUsername() {
        return username;
    }       

    public void run(){
        while(true){
            this.checkMap();
            try {   
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }    

    private void checkMap() {
        while(true){        
            if(!server.getHashmensagens().get(username).isEmpty()){
                Mensagem m = server.getHashmensagens().get(username).get(0);
                try {
                    System.out.println("mensagem enviada:"+ m);
                    output.writeObject(m);
                } catch (IOException e) {

                }
            }   
            else{
                break;
            }
        }
    }   
}

问题是我忘记在ho.start()代码中添加HandlerIn行,但是当我这样做时,服务器会一次又一次地抛出此异常:

java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.io.ObjectInputStream$PeekInputStream.peek(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.peek(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at HandlerIn.run(HandlerIn.java:27)

我该如何解决这个问题?

__________________________________ EDIT_______________________________________ 这是客户端代码:

public class Login {
        public static int PORT = 8888;
        public void init(){
        (...)
        Client c = null;
        b = true;
        try {
            Socket socket = new Socket(InetAddress.getByName(null), PORT);
            c = new Client(socket, v[1], v[0]);                                 
        } catch (IOException e1) {
            System.out.println("Conexao falhada");
        } 
        if(c != null){
        QuequeApp quequeapp = new QuequeApp();
        Mensagem m = new Mensagem(c.getUsername(), null, null);
        try {
            c.getOutput().writeObject(m);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        (...)
    }
}

客户端在登录后立即发送消息,以便他可以向服务器表明身份。

在这方面,控制台一遍又一遍地显示:

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.peek(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.peek(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at HandlerIn.recebeMensagem(HandlerIn.java:31)
at HandlerIn.run(HandlerIn.java:75)

注意:此Handler不是服务器之一。它的第31行是m = (Mensagem)input.readObject();

0 个答案:

没有答案