I'm trying to create a client-server Java application, in which 4 users are connected, however I have some issues with the communication and message exchange between the Server and Client classes.
Here is the code for the Server:
public class Server { private static final int port = 9991; private static final String serverHost = "localhost"; private static ServerSocket serverSocket; private static Socket socket; private static DataInputStream serverInput; private static DataOutputStream serverOutput; private static String message; private static int noOfUsers = 0; public static void main(String args[]){ try{ serverSocket = new ServerSocket(port); System.out.println("Server is up (Port: " + port + ")"); while (true){ socket = serverSocket.accept(); serverInput = new DataInputStream(socket.getInputStream()); serverOutput = new DataOutputStream(socket.getOutputStream()); message = serverInput.readUTF(); if (message.equals("C_Message1")){ System.out.println("Client has connected!"); serverOutput.writeUTF("S_Message1"); } else if (message.equals("C_Message2")){ if (noOfUsers <= 3){ serverOutput.writeUTF("S_Message2"); noOfUsers++; } else { serverOutput.writeUTF("S_Message3"); System.out.println("User rejected"); } } } } catch (IOException e){ System.err.println(e.getMessage() + " -> " + e.getCause()); } } } and here is the code for Client class:
public class Client implements Runnable { private static final int serverPort = 9991; private static final String serverHost = "localhost"; private static Socket socket; private static DataInputStream clientInput; private static DataOutputStream clientOutput; private static String message; private static int userID; @Override public void run() { try { socket = new Socket(serverHost, serverPort); System.out.println("Connection succesfull."); clientInput = new DataInputStream(socket.getInputStream()); clientOutput = new DataOutputStream(socket.getOutputStream()); clientOutput.writeUTF("C_Message1"); System.out.println("Connected to server!"); while (true){ message = clientInput.readUTF(); if (message.equals("S_Message1")){ clientOutput.writeUTF("C_Message2"); } else if (message.equals("S_Message2")){ System.out.println("Accepted on table!"); } else if (message.equals("S_Message3")){ System.out.println("Rejected"); } } } catch (UnknownHostException e) { System.out.println("Cannot find host."); } catch (IOException e) { System.out.println("IO Exception thrown"); } } } and I'm creating an instance of Client inside my GUI class,
Client client = new Client(); Thread clientThread = new Thread(client); clientThread.start(); The problem is that the message exchange does not work properly. I'm sending C_Message1 from Client to Server, when server receives this message, it sends back S_Message1 to client, and then the Client sends C_Message2 to Server and so on. However the message exchange, somehow stops when the client is trying to send C_Message2.
acceptis inside thewhileloop. This means that after the new message, the server is accepting a new connection, the old socket is abandoned, and it will never receive any messages from it anymore.socket.accept()can be placed. Thanks for your reply.socket.accept()until a user connects... You should have a separate loop for accepting, and a separate loop for handling a single session - preferably in another thread (or you won't be able to accept another user).