为什么跳过第三个printstream println()命令?

时间:2018-01-10 23:17:14

标签: java printstream

我正在编写一个服务器/客户端程序,其中服务器向客户端发送消息,反之亦然。除了out.println方法中的printRemoteAddress()语句外,它的工作方式非常完美。 "HELLO1""HELLO2"打印给客户,但是"HELLO3"没有。

我的问题是,为什么"HELLO3"不打印给客户,但前两个呢?

服务器代码:

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

客户代码:

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

}

1 个答案:

答案 0 :(得分:2)

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

Bzzzzzzzzt。这迭代两次,而不是三次。它应该是

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

事实上,根本不用硬连线就更有意义了:

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

println()未被'跳过',但PrintStream已缓存,并且不会在换行符上自动刷新。您可以使用第二个参数构建它,或者根据需要调用flush(),或者使用PrintWriter

您还应该关闭PrintStream/PrintWriter,而不是Socket