0

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.

3
  • 1
    Your accept is inside the while loop. 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. Commented Sep 9, 2015 at 10:58
  • Even if just a single user is connected to the server, the problem still exists. Moreover I can't see any other place where the socket.accept() can be placed. Thanks for your reply. Commented Sep 9, 2015 at 11:01
  • If you have just one user then it will just wait at the 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). Commented Sep 9, 2015 at 11:09

1 Answer 1

1

In a client server system like this one you have to handle two threads (at least) on client and two threads on server:

  • one thread send messages
  • one thread receive messages

It is an asynchronous that works basically as follow:

  • Sender thread on client send a message "Hello"
  • Receiver thread on server receive the message "Hello"
  • Receiver thread create a new message "I received hello message" and put it on a shared list between receiver and sender thread of the server
  • Sender thread of the server see that a new message is present on the shared list
  • Sender thread extract the message from the list and sent it to client
  • Receiver thread on the client receive a message "I received hello message"

I hope the process is clear. Good luck

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks fro your reply Davide. I'm a bit confused with the term 'shared list'. Any code-examples (not necessarily based on my code) are welcome.
Shared between threads. It means that you have to access it in a synchronized block otherwise you can have corrupt data reading it