Home » Java » database – Java Client Server menu-Exceptionshub

database – Java Client Server menu-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment


So i am trying to make a java Client Server application. On the client side I have a menu with options for the user to select. What I want the server to do then is respond to the option entered with the appropriate data. Here is my code, where am I going wrong?(I also have a players database class created with functions but no need to include for answer to my question)

  1. Server class
public class Server {

     * @param args the command line arguments
    public static void main(String[] args) throws SQLException {
        try {

            System.out.println("Server started and awaiting connections....");
            // Create a server socket
            ServerSocket serverSocket = new ServerSocket(8000);

            // Start listening for connections on the server socket
            Socket connectToClient = serverSocket.accept();

            // Create an input stream to get data from the client
            DataInputStream isFromClient = new DataInputStream(

            // Create an output stream to send data to the client
            DataOutputStream osToClient = new DataOutputStream(

            InetAddress clientInetAddress = connectToClient.getInetAddress();

            System.out.println("Clients host name is " + clientInetAddress.getHostName());
            System.out.println("Clients IP address is " + clientInetAddress.getHostAddress());

            // Continuously read from the client and process it,
            // and send result back to the client
            while (true) {

                // Read a string from the input stream
                int option = isFromClient.readInt();

                // Display option on console
                System.out.println("Option received from client: " + option);

                // Compute option

                //int x = (int) option1();
                if (option == 1){
                       List<players> list = PlayersDB.getAllplayers();
                    //System.out.println("Number of players " + list.size());
                    int x = list.size();
                // Send the result to the client

                // Print the result to the console
                System.out.println("Answer to opton is: " + x);

            }//end while
        }//end try
        catch (IOException ex) {
        }//end catch

2.Client Class

public class Client {

     * @param args the command line arguments
    public static void main(String[] args) {

                try {

            Scanner scanner = new Scanner(System.in);

            // Create a socket to connect to the server
            // Socket connectToServer = new Socket("", 8000);
            Socket connectToServer = new Socket("localhost", 8000);

            // Create an input stream to receive data from the server
            DataInputStream isFromServer = new DataInputStream(

            // Create an output stream to send data to the server
            DataOutputStream osToServer
                    = new DataOutputStream(connectToServer.getOutputStream());

            // Continuously send radius and receive area from the server
            while (true) {
                // Read the radius from the keyboard
                System.out.println("Please select an option:");
                System.out.println("1 - Number of players:");
                System.out.println("2 - Number of English players:");

                int option = scanner.nextInt();

                // Send the option selected to the server

                // Get answer from the server
                int answer = isFromServer.readInt();

                // Print answer on the console
                System.out.println("Answer to the selected option is " + answer);

            }//end while
        }//end try
        catch (IOException ex) {
        }//end catch
How to&Answers:

Change order of input/output stream opening on the client or on the server.
If you opening input stream on one side, current thread will be blocked until the on the other end will be opened output stream and vice versa.
I suppose now your programs just hang.