user2867798
user2867798

Reputation:

Client-Server communication in Java

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.

Upvotes: 0

Views: 2360

Answers (1)

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26946

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

Upvotes: 1

Related Questions