user2280288
user2280288

Reputation: 95

a client/server program using java won't work

I'm trying to create a client/server program with java.

when the client connect to the server, the server will show him a message to enter the first value when the user write the first value the server sends him a message to write the sencd value when the user write the second value the server will show him a list of operations ans wait until the client write the number of the operation and then the server will send him the result of this operation.

When I write the program's code and run the server and then the client, it doesn't do any thing the server is blocked from doing anything, also the client.

this is the code I tried :

for the client :

import java.net.*;
import java.util.Scanner;
import java.io.*;


public class Client {

    final static String ADRSS = "localhost";
    final static int PORT = 1234;
     static Socket s = null;

    public static void main(String[] args) {

        try{


                Scanner cn = new Scanner(System.in);
                s = new Socket(ADRSS, PORT);
                PrintWriter out = new PrintWriter(s.getOutputStream());
                BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
                in.readLine();
            out.println(cn.nextLine());
            out.flush();
            in.readLine();
            out.println(cn.nextLine());
            out.flush();
            in.readLine();
            out.println(cn.nextLine());
            out.flush();

            System.out.println("Res = " + in.readLine());
            out.flush();

        }
        catch(IOException e){e.printStackTrace();

        }


    }

}

for the server:

  import java.net.*;
import java.io.*;

public class Server {


    final static int PORT = 1234;
    private static ServerSocket server;

    public static void main(String[] args) {
        Socket s = null;
        try {
        server = new ServerSocket(PORT);
        s = server.accept();
        PrintWriter out = new PrintWriter(s.getOutputStream());
        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        out.println("Donner le premier nombre : ");
        out.flush();
        double n1 = Double.parseDouble(in.readLine());
        out.println("Donner le deuxiéme nombre : ");
        out.flush();
        double n2 = Double.parseDouble(in.readLine());

        out.println("Donner l'op : ");
        out.flush();

        String choix = in.readLine();
        String res = null;

        switch(choix){

            case "1" :
                res = String.valueOf(n1 + n2);
                break;
            case "2" : 
                res = String.valueOf(n1 - n2);
                break;
            case "3" :
                res = String.valueOf(n1 * n2);
                break;
            case "4" :
                res = (n2 == 0) ? "Impossible d'éfectuer l'op" : String.valueOf(n1 / n2);
                break;
            default :
                res = "erreur";
            }

        out.println(res);
        out.flush();

        }catch(IOException e) {
            e.printStackTrace();
        }finally{
            try{
                s.close();
            }catch(IOException e){e.printStackTrace();}
        }

    }

}

Upvotes: 1

Views: 538

Answers (2)

Dima
Dima

Reputation: 8652

first, after every print in the server, add

out.flush();

second, you are asking for nextLine() but printing without \n , either add \n to end of each string or use out.println

Upvotes: 0

TNW
TNW

Reputation: 726

PrintWriter doesn't flush output after you use regular print (refer to documentation of PrintWriter). You'd have to flush it manually. However, the real reason is your client waits for a line with newline, which never happens. Changing to out.println on the server side should make this running, also covering the flushes.

Upvotes: 1

Related Questions