java.net.SocketException:尝试将对象从客户端传递到服务器时重置连接

时间:2019-03-27 00:05:23

标签: java sockets tcp

我试图通过客户端的ObjectOutputStream类传递对象,并通过ObjectInputStream接收对象,问题是java.net.SocketException:连接重置错误出现,消除了客户端和服务器之间的对象过去,问题是它可以解决,但我不知道会是什么错误。

在服务器上有客户端代码之前,我可以正常工作,反之亦然,但是现在,我更改了代码的这些部分后,就无法正常工作了。这些错误在模块服务器上的这些行中跳转:

ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
Computer c = (Computer) ois.readObject();

客户

private void startClient() {
    DataInputStream in = null;
    DataOutputStream out = null;
    Socket socket = null;
    try {

        socket = new Socket(ip, port);

        in = new DataInputStream(socket.getInputStream());
        out = new DataOutputStream(socket.getOutputStream());
        //MANDAMOS EL NUMERO EN RANGO HACIA LOS SERVIDORES
        out.writeInt(n);
        out.flush();
        //LEEMOS EL TIEMPO ENVIADO POR EL SERVIDOR
        tiempo = in.readLong();
        System.out.println(tiempo);

        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        Computer c = (Computer) ois.readObject();
        synchronized (main) {
            main.add(c);
        }
        ois.close();

    } catch (IOException ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            out.close();
            in.close();
            socket.close();
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

服务器

private void startServer(){

    DataOutputStream out = null;
    DataInputStream in = null;
    ServerSocket ss = null;
    try {

        Socket socket = null;
        ss = new ServerSocket(port);
        System.out.println("Esperando conexion");
        socket = ss.accept();
        in = new DataInputStream(socket.getInputStream());
        int n = in.readInt();
        long time = encontrarPrimos(n);
        out = new DataOutputStream(socket.getOutputStream());
        out.writeLong(time);
        out.flush();

        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        Computer c = new Computer(id, Computer.getLocalIp(), time, Computer.getUserDomainSO());
        oos.writeObject(c);

        oos.close();
        in.close();
        out.close();
        socket.close();


    } catch (IOException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
    }
}

计算机类

public class Computer implements Serializable{
    private int id;
    private String ip;
    private long time;
    private String userDomain;
    public Computer(int id, String ip, long time, String userDomain) {
        this.id = id;
        this.ip = ip;
        this.time = time;
        this.userDomain = userDomain;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public String getUserDomain() {
        return userDomain;
    }

    public void setUserDomain(String userDomain) {
        this.userDomain = userDomain;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 59 * hash + this.id;
        hash = 59 * hash + Objects.hashCode(this.ip);
        hash = 59 * hash + (int) (this.time ^ (this.time >>> 32));
        hash = 59 * hash + Objects.hashCode(this.userDomain);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Computer other = (Computer) obj;
        if (this.id != other.id) {
            return false;
        }
        if (this.time != other.time) {
            return false;
        }
        if (!Objects.equals(this.ip, other.ip)) {
            return false;
        }
        if (!Objects.equals(this.userDomain, other.userDomain)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Computer{" + "id=" + id + ", ip=" + ip + ", time=" + time + ", userDomain=" + userDomain + '}';
    }

    public static String getLocalIp(){
        try {
            InetAddress localhost = InetAddress.getLocalHost();
            return localhost.getHostAddress().trim();
        } catch (UnknownHostException ex) {
            Logger.getLogger(Computer.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static String getUserDomainSO() {
        String operatingSystem = System.getProperty("os.name");

        if ("Linux".equals(operatingSystem) || "Mac OS X".equals(operatingSystem)) {
            return System.getProperty("user.name");
        } else if ("Windows".equals(operatingSystem)) {
            return System.getenv("USERDOMAIN");
        } else {
            throw new RuntimeException("Unsupported operating system.");
        }
    }

}

下一个错误:

java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:210)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2663)
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2679)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3156)
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:862)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:358)
    at Client.startClient(Client.java:71)
    at Client.run(Client.java:44)
    at java.lang.Thread.run(Thread.java:748)

2 个答案:

答案 0 :(得分:2)

我对您的客户端代码进行了少量修改,并且可以正常工作。我不在这里使用计算机对象。相反,我使用了String cast。

private void startClient() {
    DataInputStream in = null;
    DataOutputStream out = null;
    Socket socket = null;
    try {
        InetAddress host = InetAddress.getLocalHost();
        socket = new Socket(host.getHostName(), 9876);

        in = new DataInputStream(socket.getInputStream());
        out = new DataOutputStream(socket.getOutputStream());

        //MANDAMOS EL NUMERO EN RANGO HACIA LOS SERVIDORES
        out.writeInt(1000);
        out.flush();

        //LEEMOS EL TIEMPO ENVIADO POR EL SERVIDOR
        long tiempo = in.readLong();
        System.out.println(tiempo);
        String str;
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        if ((str = (String) ois.readObject()) != null) {
            System.out.println(str);
        }
        ois.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    } finally {
        try {
            out.close();
            in.close();
            socket.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

答案 1 :(得分:-1)

在测试了不同的解决方案之后,主要的错误不是代码,而是安装在运行客户端的计算机上的JVM,因此解决方案是使用以前安装的相同版本从0开始安装Java。

>