r/javahelp • • Feb 02 '26

Unsolved Response given in wrong order in thread and socket program

I'm doing an exercize for uni, I have to write a program in which the clients send the server an integer, and the server then creates a thread for each client in which it reads the integer sent, and sends to the client the sum of all integers received up until that point.

This is my code

Client:

public class Client {
    public static void main(String[] args) {
        try{
            ArrayList<Socket> sockets = new ArrayList<>();
            for (int i = 0; i<20; i++){

                Socket socket = new Socket("localhost", 9000);
                sockets.add(socket);
                ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());


                Random rand = new Random();
                int r = rand.nextInt(0, 100);
                out.writeObject(100);
            }

            for (int i = 0; i<20; i++){
                Socket socket = sockets.get(i);
                ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                System.out.println(Integer.parseInt(in.readObject().toString()));
                socket.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}

Server:

public class Server {
    public static AtomicInteger sum = new AtomicInteger(0);

    public static void main(String[] args) {
        try{
            ServerSocket serverSocket = new ServerSocket(9000);

            while(true){
                Socket socket = serverSocket.accept();

                ServerThread st = new ServerThread(socket);
                st.start();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

ServerThread:

public class ServerThread extends Thread{

    Socket socket;

    public ServerThread(Socket s){
        socket = s;
    }



    public void run(){
        try{
            ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream in = new ObjectInputStream(socket.getInputStream());

            int add = Integer.
parseInt
(in.readObject().toString());


            out.writeObject(Server.
sum
.addAndGet(add));
            socket.close();
        }
        catch (IOException | ClassNotFoundException e) {

            throw new RuntimeException(e);
        }

    }
}

The issue I'm experiencing is that in the client class, when it goes to print the results received, they're printed in the wrong order, so instead of 100, 200, 300 etc. I get 1900, 900, 1200 etc. All the "steps" show up and there's no duplicates though.

The strange thing is that if I run the client again without terminating the server, it actually continues in the correct order, so I get 2100, 2200, 2300 etc.

Am I doing something wrong?

1 Upvotes

15 comments sorted by

View all comments

1

u/ShoulderPast2433 Feb 02 '26

Dude.. what tf is this formatting?

why are you cutting the lines like that???

1

u/Dependent_Finger_214 Feb 02 '26

Sorry, I just copypasted from my code and it ended up like this...

1

u/0b0101011001001011 Feb 05 '26

Did he edit it? The code is perfectly formatted in code blocks at this point.

Hint: scroll right.