【问题标题】:Java Socket Server - Client; Stuck on Server sideJava 套接字服务器 - 客户端;卡在服务器端
【发布时间】:2020-11-18 22:46:38
【问题描述】:

这是我第一次在我的代码中找不到问题/错误,所以现在我问 stackoverflow xD

我目前正在学习如何编写一个简单的服务器 - 客户端网络,以了解 Java 中的套接字和服务器套接字是如何工作的。因此,我编写了一个由 Server、Client 和 Handler 类组成的“程序”。 Handler 类负责接收 Client 的消息并发送响应。从服务器发送到客户端工作正常,客户端收到消息。但是,当客户端向服务器发送消息时,它不会收到任何内容。我正在使用的 BufferedReader 卡在 readLine() 请求上。

//This is the broken down version of what is happening in my Handler class
ServerSocket server = new ServerSocket(port); //These two lines of code actually happen in the Server class
Socket client = server.accept();              //but to simplify it I put them here
//Setting up the IO
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
//The Handler is waiting for the Reader to be ready and then prints the input
//Additionally, it sends a confirmation to the client
while(true) {
    String input;
    if(in.ready()){
        if ((input = in.readLine()) != null) {
            System.out.println(input);
            out.println("Message Received");
            if(input=="close") break;
        }
    }
}

//Expected output: "Message Received"
//Actual ouput: none, it gets stuck at in.ready() or in.readLine()

当我从客户端发送消息时,它应该只打印消息并向客户端发送确认信息,但如果我删除第一个 if-陈述。我使用 IntelliJ 对其进行调试,并且 InputStream 不包含readLine() 所期望的任何消息或回车符。我发现这很奇怪,因为服务器和客户端类的发送和接收部分(大部分)是相同的。

我能想到的唯一可能是导致此问题的原因是客户端在发送消息时遇到问题。

//This is the broken down version of what is happening in my Client class
Socket client = new Socket();
client.connect(new InetSocketAddress("localhost",port));
//Setting up the IO
Scanner scanner = new Scanner(System.in); //I am using a Scanner for the message inputs
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
String input;
System.out.println("Enter the first Message: ");
while ((input = scanner.nextLine()) != null) {
    String inServer;
    System.out.println(input); //The input is correct
    out.println(input); //I am suspecting it has something to do with this line or the PrintWriter
    //This part works perfectly fine here while it does not in the Handler class
    if (in.ready()) {
        if ((inServer = in.readLine()) != null) {
            System.out.println(inServer);
        }
    }
    System.out.println("Enter next Message: ");
}
Expected output: inServer
Actual output: inServer

如您所见,这部分的一般设置与 Handler 类中的相同,但是在发送到服务器时似乎出现了问题。我不知道它是服务器(我不这么认为,因为用于接收消息的相同代码在客户端类中工作得很好)还是客户端,在这种情况下它必须是 PrintWriter 的问题或类似的东西。

我已经在 stackoverflow 上查看了其他/类似的问题,但没有找到任何可以解决我的问题的东西。 如果有人想详细复制所有内容,则类的完整代码:(Pastebin 链接)

Server class

Client class

Handler class

【问题讨论】:

  • 打印后可以调用 out.flush() 吗?
  • 我在实例化 PrintWriter 时将 autoFlush 设置为 true,所以我已经实现了
  • in.ready() 在这里没有做任何有用的事情,它只是在一个紧密的循环中浪费 CPU 时间。只需将in.ready() if 语句中的代码移到该语句之外并删除if
  • 您的代码对我有用。当客户端发送消息时,例如“Hello World”,服务器打印[Server] received Message: Hello World
  • 您是否对代码进行了任何更改,或者您是否只是从 pastebin 复制了类?

标签: java sockets


【解决方案1】:

嗯,看来问题已经回答了…… 似乎它与代码所在的 IntelliJ 项目有关。我不得不将它移到一个新项目中,现在它可以工作了。 现在问题已解决,如果有人想将此代码用作服务器-客户端系统的基础,我会让 pastebin-links 继续工作。

【讨论】:

    【解决方案2】:

    我的建议是使用实现 Reader 的类的 read() 方法(BufferedReader 就是其中之一)。语法是这样的:

    String data = "";
    int i;
    while ((i = in.read()) != -1){
        data += (char)i;
    }
    

    这种方式可靠多了,也不存在回车问题。

    【讨论】:

      猜你喜欢
      • 2012-06-05
      • 1970-01-01
      • 2017-02-25
      • 2012-06-24
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 2016-02-06
      • 2017-08-23
      相关资源
      最近更新 更多