-1

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();    
    }   
}

}

Omar N
  • 1,720
  • 2
  • 21
  • 33
  • You need to check the result of `readLine()` for null before you do anything else with it, and close the socket if you get it. – user207421 Jan 10 '18 at 23:43
  • 1
    In your client code: `for (int i = 1; i <3; i++) {` only iterates 2 times. Write it out on paper and see. Either start with i at zero or use <= – Erwin Bolwidt Jan 11 '18 at 00:54
  • Thank you. I changed it to <= and that fixed the problem. As EJP stated, the issue was with the client code and not the server code. – Omar N Jan 11 '18 at 01:01

1 Answers1

2
 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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Even when I construct it with autoflosh (`out = new PrintStream(sock.getOutputStream(), true);`) it still doesn't work. Nor does putting a `out.flush()` command in between each `out.println()` statement. – Omar N Jan 10 '18 at 23:41
  • Then there must be something wrong with your client code, which you haven't posted. Edit it into your question. – user207421 Jan 10 '18 at 23:42