ClientSocket 不监听

ClientSocket NOT listening

我目瞪口呆。

我从 https://docs.oracle.com/javase/tutorial/networking/sockets/examples/EchoServer.java

获取了代码

对于服务器。和 https://docs.oracle.com/javase/tutorial/networking/sockets/examples/EchoClient.java

为客户。我做了一些小改动。主要是为了没有来回回声。相反,服务器应该不断地以 2 秒的延迟发送相同的字符串。但我就是不明白为什么客户不工作。 它发送异常消息: 无法 I/O 连接到 127.0.0.1 我 运行 服务器:java 6788 和客户端:127.0.0.1 6788 我尝试了其他端口。

我在 eclipse 中执行此操作,因此我在 运行 设置 类 之前在 Runconfiguration 中设置了参数。我先启动服务器。我在 eclipse 之外的终端尝试过。没有什么能让它发挥作用。 基本上,客户端应该连接到服务器并使用 System.out.println() 输出服务器依次输出到客户端的内容。但是没有任何反应。 怎么了?

客户:

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

public class EchoClient {
    public static void main(String[] args) throws IOException {

        if (args.length != 2) {
            System.err.println(
                "Usage: java EchoClient <host name> <port number>");
            System.exit(1);
        }

        String hostName = args[0];
        int portNumber = Integer.parseInt(args[1]);

        try (
            Socket echoSocket = new Socket(hostName, portNumber);
            BufferedReader in =
                new BufferedReader(
                    new InputStreamReader(echoSocket.getInputStream()));

        ) {
            String userInput;

            while (true) {
                System.out.println("recieved: " + in.readLine());
            }
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host " + hostName);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to " +
                hostName);
            System.exit(1);
        } 
    }
}

服务器:

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

public class EchoServer {
    public static void main(String[] args) throws IOException {

        if (args.length != 1) {
            System.err.println("Usage: java EchoServer <port number>");
            System.exit(1);
        }

        int portNumber = Integer.parseInt(args[0]);
        System.out.println(args[0]);
        InetAddress add = InetAddress.getLocalHost();
        System.out.println(add.getHostAddress());
        try (
            ServerSocket serverSocket =
                new ServerSocket(Integer.parseInt(args[0]));

            Socket clientSocket = serverSocket.accept();     
            PrintWriter out =
                new PrintWriter(clientSocket.getOutputStream());                   

        ) {
            while (true) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                out.println("HELLO!");
            }
        } catch (IOException e) {
            System.out.println("Exception caught when trying to listen on port "
                + portNumber + " or listening for a connection");
            System.out.println(e.getMessage());
        }
    }
}

您必须将答案发送给客户。

添加一个out.flush();

while (true) {
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    out.println("HELLO!");
    out.flush();
}

如评论中所述,我最终找到了解决方案。 BufferedReader.readLine() "blocked"。从文件中读取时,如果我理解正确的话,它 returns 读取一个换行符后一行。 但是由于它是 "A steady flow" 来自没有换行符的服务器,它只是保持 "reading" 并且从未返回 String.

然后我尝试使用 BufferedReader.read() 方法,该方法逐个字符读取,并在每个字符后使用 returns(因此从不阻塞)。然后它在每个字符到达时打印它,它还监听从服务器发送的换行符,一旦读取的字符等于换行符,它就会打印换行符。有点模仿我从原始问题中期望的 "read line" 行为。

客户端读取部分:

    while(true) {
        character = (char) reader.read();
        if(Character.isISOControl(character)) {
            System.out.println();
        }
        else {
            System.out.printf("%c", character);
        }           
    }

发送部分服务器:

private String message = "HELLO\n";
...
while(true) {
            try {
                Thread.sleep(2000);
                writer.write(message);
                writer.flush();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }