Skip to content
Advertisement

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;
    try{
        sockConn = new ServerSocket();
        InetSocketAddress isa = new InetSocketAddress(portServ);//port + wildcard.
        sockConn.bind(isa);
        System.out.println("Server Launched !");
        int nbForme = 0;
        int compteur = 0;
        while(true){
            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());
            os.writeDouble(forme.perimetre());
            os.flush();
            os.writeDouble(forme.aire());
            os.flush();
            //compteur++;
            
            //System.out.println("Nombre de forme demande atteint");
            //is.close();
            //os.close();
             
        }

And my client :

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

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

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

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement