Skip to content

Java Server Socket Socket program stuck while reading

i have 2 programm one is a server and the other the client , i have some other class who represent geomtric shape. In the client with the scanner the user write the caracteristics of the shape width,length for a rectangle as an example. the he create an object and send it to the server , the server receive the object calculate area and perimeter and send them to the client. This should happen until the user press enter in the client instead of caracteristics. And i don’t know why but the first loop work good but the second one i type my caracteristics and then nothing happen anymore and my programms are stuck . Maybe you can see my error here are the 2 programm :

Ps: i’m French so sorry for the french word use for my variable’s name Server:

ServerSocket sockConn = null;
    Socket sockComm = null;
        sockConn = new ServerSocket();
        InetSocketAddress isa = new InetSocketAddress(portServ);//port + wildcard.
        System.out.println("Server Launched !");
        int nbForme = 0;
        int compteur = 0;
            sockComm = sockConn.accept();
            ObjectInputStream is = new ObjectInputStream(sockComm.getInputStream());
            ObjectOutputStream os = new ObjectOutputStream(sockComm.getOutputStream());
            //nbForme = is.readInt();
            //System.out.println("nombre de forme a traiter recu : "+nbForme);              
            FormeGeo forme = (FormeGeo)is.readObject();
            System.out.println("Forme recu "+forme.toString());
            //System.out.println("Nombre de forme demande atteint");

And my client :

            String ipServ = args[0];
        sockComm = new Socket();
        InetSocketAddress connect= new InetSocketAddress(ipServ,portServ);
        //          System.out.print("Entrez un nombre de forme a saisir : ");
        //sc = new Scanner(;
        //nbForme = sc.nextInt();
        //sc = null;
        //System.out.println("Envoi du nombre de forme a traiter");

            System.out.println("Saisissez les valeurs d'une forme ou appuyer sur entrer pour stopper le programme");
            sc = new Scanner(;
            line = sc.nextLine();
            os = new ObjectOutputStream(sockComm.getOutputStream());
            is = new ObjectInputStream(sockComm.getInputStream());
                System.out.println("Fin du programme");
            }else if(!line.contains(",")){
                rayon = Double.parseDouble(line);
                    System.out.println("Longueur du rayon doit ĂȘtre positive");
                FormeGeo rond = new Rond(rayon);
                String[] numbers = line.split(","); 
                    longueur = Double.parseDouble(numbers[1]);
                    largeur = Double.parseDouble(numbers[0]);
                    FormeGeo rec= new Rectangle(largeur,longueur);
                    System.out.println("Erreur trop d'arguments pour un rectangle !");

            double perimetre = is.readDouble();
            double aire = is.readDouble();
            System.out.println("perimetre et  aire renvoye par le serveur respectivement : "+perimetre+" et "+aire);


Your server socket does the following:

  • Wait for a connection to happen, and accept it. (this is sockConn.accept())
  • Receive a request from that connection
  • Send a response to that connection
  • Wait for a connection to happen, and accept it.
  • Receive a request from that connection
  • Send a response to that connection

i.e. your server only expects one request per connection.

Your client does this:

  • Connect to the server
  • Send a request
  • Receive a response
  • Send a request
  • Receive a response

When your client is waiting for the 2nd response, your server is waiting for the 2nd connection, so nothing happens.

You can fix it by doing one of these things:

  • Make the client start a new connection for every request
  • Make the server handle many requests on the same connection (instead of waiting for a new one)
  • Make the server start a new thread for every connection, so it can process several connections at the same time