Search code examples
javaprintstream

Why is third printstream println() command skipped?


I've got a server/client program that I am writing in which a server sends messages to a client and vise versa. It works perfectly, except for the out.println statements in the printRemoteAddress() method. "HELLO1" and "HELLO2" print to the client, but "HELLO3" does not.

My question is, why does "HELLO3" not print to the client, but the first two do?

SERVER CODE:

class Worker extends Thread {

Socket sock;
Worker (Socket s) {sock = s;}


    public void run() {

        PrintStream out = null;
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            out = new PrintStream(sock.getOutputStream());

            try {
                String name;
                name = in.readLine();
                System.out.println("Looking up " + name);
                printRemoteAddress(name, out);
            } catch (IOException x) {
                System.out.println("Server read error");
                x.printStackTrace();
            }
            sock.close();
        } catch (IOException ioe) {System.out.println(ioe);}
    }

        static void printRemoteAddress (String name, PrintStream out) {
            try {
                out.println("HELLO1");
                out.println("HELLO2");
                out.println("HELLO3");
                InetAddress machine = InetAddress.getByName(name);
            } catch(UnknownHostException ex) {          
                out.println ("Failed in attempt to look up " + name);
            }
        }




public class InetServer {

public static void main(String a[]) throws IOException {
    int q_len = 6;
    int port = 2000;
    Socket sock;

    ServerSocket servsock = new ServerSocket(port, q_len);

    System.out.println
        ("Inet server 1.8 starting up, listening at port 2000.\n");
    while (true) {
        sock = servsock.accept();
        new Worker(sock).start();
    }   
}

CLIENT CODE:

public class InetClient {

public static void main (String args[]) {
    String serverName;
    if (args.length < 1) serverName = "localhost";
    else serverName = args[0];

    System.out.println("Inet Client, 1.0.\n");
    System.out.println("Using server: " + serverName + ", Port: 2000");

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    try {
        String name;
        do {
            System.out.print("Enter hostname of IP address: ");
            System.out.flush();
            name = in.readLine();
            if (name.indexOf("stop") < 0)
                getRemoteAddress(name, serverName);
        } while (name.indexOf("stop") < 0);
        System.out.println("Process stopped.");;
    } catch (IOException x) {x.printStackTrace();}
}

static void getRemoteAddress (String name, String serverName) {
    Socket sock;
    BufferedReader fromServer;
    PrintStream toServer;
    String textFromServer;

    try {
        sock = new Socket(serverName, 2000);

        fromServer = new BufferedReader (new InputStreamReader(sock.getInputStream()));

        toServer = new PrintStream(sock.getOutputStream());
        toServer.println(name);
        toServer.flush();

        for (int i = 1; i <3; i++) {
            textFromServer = fromServer.readLine();
            if (textFromServer != null) System.out.println(textFromServer);
        }
        sock.close();
    } catch (IOException x) {
        System.out.println("Socket error.");
        x.printStackTrace();    
    }   
}

}


Solution

  •  for (int i = 1; i <3; i++) {
    

    Bzzzzzzzzt. This iterates twice, not three times. It should be

     for (int i = 1; i <= 3; i++) {
    

    In fact it would make more sense not to hardwire the 3 in at all:

    while ((textFromServer = fromServer.readLine()) != null {
        System.out.println(textFromServer);
    }
    

    The println() isn't 'skipped', but PrintStream is buffered and doesn't auto-flush on newlines. You can construct it with a second parameter so as to do that, or call flush() as appropriate, or use a PrintWriter.

    You should also close the PrintStream/PrintWriter, not the Socket.