Question

Yestarday I wrote a post about Java and Sockets, and today I'm still here because I'm having an issue with BufferedReaders.
I searched some questions here in StackOverflow and I understand the problem, but I can't fix it
My "application" has got two parts: a server and a client, and the scope of the application is to execute MS-DOS commands on the machine where the server is running (the commands are sent by the client).
Now the code (I will post the total code because it's easier to understand, I will put a comment in non-working part of the code)

Server:

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

public class TCPCmdServer {
    public int port;
    public ServerSocket server;
    public final String version = "Beta 1.0";

    TCPCmdServer(int port) {
        this.port = port;
        if (!createServer())
            System.out.println("Cannot start the server");
        else {
            System.out.println("**********************************************");
            System.out.println("Command executer, server version: " + version);
            System.out.println("Server running on port " + port);
            System.out.println("Code by luc99a alias L99");
            System.out.println("**********************************************");
        }
    }

    public boolean createServer() {
        try {
            server = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    public static void main(String[] args) {
        TCPCmdServer tcp = new TCPCmdServer(5000);

        while (true) {

            Socket socket = null;
            BufferedReader in = null;
            BufferedWriter out = null;

            try {
                socket = tcp.server.accept();
                System.out.println("A client has connected");
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                out.write("Welcome on the server... type the commands you like, type END to close the connection\n");
                out.flush();
            } catch (IOException exc) {
                exc.printStackTrace();
            }

            if (socket != null && in != null && out != null) {
                try {
                    String cmd = null;
                    while (!(cmd = in.readLine()).equals("END")) {
                        System.out.println("Recieved: " + cmd);
                        Process p = Runtime.getRuntime().exec(cmd);
                        BufferedReader pRead = new BufferedReader(new InputStreamReader(p.getInputStream()));

                        String line;
                        StringBuilder builder = new StringBuilder();
                        while ((line = pRead.readLine()) != null) {
                            builder = builder.append(line + "\n");
                        }
                        out.write(builder.toString() + "\n");
                        //here is sent "EnD"
                        out.write("EnD \n");
                        out.flush();
                        System.out.println(builder.toString());
                        pRead.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                } finally {
                    System.out.println("Closing connection...");
                    try {
                        socket.close();
                        in.close();
                        out.close();
                    } catch (IOException excp) {
                        excp.printStackTrace();
                    }
                }
            }
        }

    }
}

And now the code for the client part

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

public class TCPCmdClient {
    public Socket socket;
    public int port;
    public String ip;
    public final String version = "Beta 1.0";

    TCPCmdClient(String ip, int port) {
        this.ip = ip;
        this.port = port;
        if (!createSocket())
            System.out.println("Cannot connect to the server. IP: " + ip + " PORT: " + port);
        else {
            System.out.println("**********************************************");
            System.out.println("Command executer, client version: " + version);
            System.out.println("Connected to " + ip + ":" + port);
            System.out.println("Code by luc99a alias L99");
            System.out.println("**********************************************");
        }
    }

    public boolean createSocket() {
        try {
            socket = new Socket(ip, port);
        } catch (IOException e) {
            return false;
        }

        return true;
    }

    public static void main(String[] args) {
        TCPCmdClient client = new TCPCmdClient("127.0.0.1", 5000);

        try {
            BufferedReader sysRead = new BufferedReader(new InputStreamReader(System.in));
            BufferedReader in = new BufferedReader(new InputStreamReader(client.socket.getInputStream()));
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(client.socket.getOutputStream()));

            String response = in.readLine();
            System.out.println("Server: " + response);

            boolean flag = true;
            while (flag) {
                System.out.println("Type a command... type END to close the connection");
                String cmd = sysRead.readLine();
                out.write(cmd + "\n");
                out.flush();
                if (cmd.equals("END")) {
                    client.socket.close();
                    sysRead.close();
                    in.close();
                    out.close();
                    flag = false;
                } else {
                    //The loop doesn't finish because the reader
                    //listens for a new line
                    //so I used the string "EnD", sent by the server to
                    //stop the loop, anyway it doesn't seem to work
                    //I put a comment in the server where "EnD" is sent
                    String output;
                    while (((output = in.readLine()) != null)) {
                        if (output.equals("EnD")) {
                            break;
                        } else {
                            System.out.println(output);
                        }
                    }

                    System.out.println("   ***************************************   ");
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}


The problem is that the BufferedReader waits for a new line forever in the while loop (I wrote a comment in the code). I tryed to stop it using a "special string", but it doesn't seem to work.

I can't change the while in

String output;
while (((output = in.readLine()) != null) && output.length > 0)
{
   //code here...
}

because in the output of the MS-DOS command (think on "ipconfig") are also present empty lines.
How could I correct it?
Thank you for your help!

Was it helpful?

Solution

your client Sends "EnD " (with a whitespace at the end) and you are comparing to "EnD" without a whitespace. So the two strings are not equal. try to send it without the white space:

out.write("EnD\n");

OTHER TIPS

Space is missing. In TCPCmdClient.java change

if (output.equals("EnD")) {

to

if (output.equals("EnD ")) {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top