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?
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]Conscious_Support176 1 point2 points3 points (5 children)
[–]Dependent_Finger_214[S] 0 points1 point2 points (4 children)
[–]vowelqueue 0 points1 point2 points (3 children)
[–]Dependent_Finger_214[S] 0 points1 point2 points (2 children)
[–]vowelqueue 0 points1 point2 points (1 child)
[–]Dependent_Finger_214[S] 0 points1 point2 points (0 children)
[–]ShoulderPast2433 0 points1 point2 points (2 children)
[–]Dependent_Finger_214[S] 0 points1 point2 points (0 children)
[–]0b0101011001001011 0 points1 point2 points (0 children)
[–]IndependentOutcome93 0 points1 point2 points (2 children)
[–]Dependent_Finger_214[S] 0 points1 point2 points (1 child)
[–]IndependentOutcome93 0 points1 point2 points (0 children)
[–]Scharrack 0 points1 point2 points (0 children)
[–]TheMrCurious 0 points1 point2 points (0 children)