繁体   English   中英

Java Socket 服务器 - 客户端; 卡在服务器端

[英]Java Socket Server - Client; Stuck on Server side

这是我第一次无法在我的代码中找到问题/错误,所以现在我问 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(in.ready()){...}部分,要么卡在if((input=in.readLine())!=null){...}如果我删除第一个 if 语句。 我使用 IntelliJ 对其进行调试,并且 InputStream 不包含readLine()预期的任何消息或回车。 我发现这真的很奇怪,因为 Server 和 Client 类的发送和接收部分(大部分)是相同的。

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

//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 类中的相同,但是在发送到 Server 时似乎出现了问题。 我不知道是服务器(我不这么认为,因为接收消息的相同代码在客户端类中工作得很好)还是客户端,在这种情况下,它必须是 PrintWriter 的问题或类似的东西。

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

服务器类

客户类

处理程序类

好吧,这个问题似乎已经得到了回答......它似乎与代码所在的 IntelliJ 项目有关。我不得不将它移到一个新项目中,现在它可以工作了。 该问题现已解决,如果有人想将此代码用作服务器-客户端系统的基础,我将保留 pastebin-links 工作。

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

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

这种方式更加可靠,并且没有回车问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM