0

I am trying to pass an object through the ObjectOutputStream class through the client, and receive it through ObjectInputStream, the problem is that the java.net.SocketException: Connection reset error appears, eliminating this past of objects between client and server, the problem is It solves, but I do not know what the error would be.

Before I worked correctly when I had the client code on the server and vice versa, but now that I change these parts of the code does not want to work. In these lines is where this error jumps, on module server:

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

CLIENT

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);
        }
    }

}

SERVER

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 {
    }
}

COMPUTER CLASS

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.");
        }
    }

}

The error is the next:

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)
8
  • Did the server throw an exception? Such as NotSerializableException for example? What is Computer? Does it have a custom writeObject() method? There's not enough information here. NB Your title is wrong. You are passing an object from the server to the client. And you can still do all the I/O with object streams, as I told you in the previous version of this question. Commented Mar 27, 2019 at 0:29
  • If I just replace you Computer c lines with System.out.println("" + (String) ois.readObject()); , it works fine. What is reason for using Computer? Commented Mar 27, 2019 at 0:36
  • @TheRoy The Computer class keeps certain key variables of the server, information of the, among others, with this it is easier for me to pass this information to the client. Commented Mar 27, 2019 at 0:46
  • 1
    The conclusion is inevitable. The problem is in the Computer class, just as it evidently was the last time when I asked the same question about the sender throwing an exception and you hastily deleted the question. And you haven't posted Computer. Again. So your question is off-topic. Commented Mar 27, 2019 at 0:47
  • The computer class is already in the question, even so, it is the same for both the client and the server. Commented Mar 27, 2019 at 0:50

2 Answers 2

2

I made few modifications to your client code and it is working. I am not using Computer object here. Instead, I have used 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();
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for answering bro. I did exactly what you did, even from the server passing a stream of String, but at the time of executing it I still delivered the same error in the client, in the line ObjectInputStream ois = new ObjectInputStream (socket.getInputStream ());
I have posted the examples that worked for me. bitbucket.org/dsajava/dsajava/src/master/stackoverflow/src/…
I managed to correct the error, apparently it was an error in the JVM, I had to reinstall Java completely and install the same version that I had before.
-1

After testing different solutions, the main error was not the code, but the JVM installed on the computer where the client was running, so the solution was to install Java from 0, using the same version that was previously installed.

2 Comments

Nonsense. The server threw an exception and exited without closing the sockets, due to your empty finally block, so the connection was reset, so you client got the exception. JVMs do not cause connection resets.
To be specific, it threw java.lang.RuntimeException: Unsupported operating system. at elpapu.Computer.getUserDomainSO(Computer.java:125), because (in my case) the value of operatingSystem was "Windows 7". I'm not aware of any Windows version that returns simply "Windows" for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.