Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I wrote a Server-Client model program in java, and it works great on eclipse. However, when I try to run the client via command line (in Windows 10), I can see the input that I type on my keyboard for stdin only after I press enter. Why is that, and how can I solve this?
Here is the client code (not the best one, I know):

public class MyEchoClient {
    public static void main(String[] args) throws IOException{
        Socket clientSocket = null; // the connection socket
        PrintWriter out = null;
        BufferedReader in = null;
        String host = args[0], msg, tmp;
        int port = Integer.decode(args[1]).intValue(), len;
        System.out.println("Connecting to " + host + ":" + port);

        try {  // Trying to connect to a socket and initialize an output stream
            clientSocket = new Socket(host, port); // host and port
            out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream(), "UTF-8"), true);
        } catch (UnknownHostException e) {
              System.out.println("Unknown host: " + host);
              System.exit(1);
        } catch (IOException e) {
            System.out.println("Couldn't get output to " + host + " connection");
            System.exit(1);
        }

        try { // Initialize an input stream
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(),"UTF-8"));
        } catch (IOException e) {
            System.out.println("Couldn't get input to " + host + " connection");
            System.exit(1);
        }

        System.out.println("Connected to server!");

        BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));
        try{
            while(true){
                while(!userIn.ready()){
                    if (in.ready()){ // if server has something to write, when client does'nt have something to write
                        tmp=in.readLine();
                        len=tmp.length();
                        System.out.print(tmp);
                        if ((len>0 && tmp.charAt(tmp.length()-1)=='>') ||  tmp.contains("UNIDENTIFIED"))
                            System.out.println();
                    }
                }
                System.out.println();
                msg = userIn.readLine();
                if (msg==null) { break; }
                out.println(msg);
                tmp=in.readLine();
                if (tmp.contains("~")) { break; }
                System.out.println(tmp);
            }
        }
        catch(IOException e){ System.out.println("Error with server connection."); }
        System.out.println("Exiting...");
        out.close();
        in.close();
        userIn.close();
        clientSocket.close();
    }
}


What I have tried:

I tried to find a replacement for the ready() function, because I think that what causes the troubles. But I didn't find equivalent function that works...
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900