简体   繁体   English

Java套接字-坚持从服务器读取数据

[英]Java Sockets - Hang on reading data from server

I'm currently working on a little Client/Server task with using sockets. 我目前正在使用套接字来处理一个小的客户端/服务器任务。 Sadly I the client hangs when it is supposed to read the "200 OK File Created" sent by the server. 可悲的是,当客户端应读取服务器发送的“ 200 OK File Created”时,该客户端挂起。 Is there anything i overlooked? 有什么我忽略的吗?

Client: 客户:

public HTTPClient(InetAddress adress, int portnumber, String filename) throws IOException {
    socket = new Socket(adress, portnumber);
    input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    output = new PrintWriter(socket.getOutputStream());
    this.filename = filename;
}

public void sendPutRequest() throws IOException {
    output.println("PUT /" + filename + " HTTP/1.0");
    output.flush();
    File myFile = new File(this.filename);
    if (myFile.exists()) {
        for (String string : Files.readAllLines(myFile.toPath())) {
            output.println(string);
        }
        output.flush();
        String line;
        while ((line = input.readLine()) != null) {
            System.out.println(line);
        }
    } else {
        throw new IOException("File not found");
    }
}

Server: 服务器:

  try (Socket client = this.socket.accept(); 
    BufferedReader in = new BufferedReader(
    new InputStreamReader(client.getInputStream()));
    PrintWriter out = new PrintWriter(client.getOutputStream())) {

    String lineIn = in.readLine();
    if (lineIn.contains("PUT")) {
        String filename = lineIn.split(" ")[1].substring(1);
        List<String> filedata = new ArrayList<>();
        String line;
        while ((line = in.readLine()) != null) {
            filedata.add(line);
            System.out.println(line);
        }
        writeToFile(filename, filedata);
        out.println("200 OK File Created");
        out.flush();
    }
}

Your server is reading from the connection until it is closed (only in such a case in.readLine() will return null ). 您的服务器正在从连接中读取数据,直到关闭为止(仅在这种情况下in.readLine()将返回null )。

However your client does not close the connection to the server. 但是,您的客户端不会关闭与服务器的连接。 Therefore the server is stuck in the while loop. 因此,服务器被困在while循环中。

Solution: You have to close the output stream after sending the request. 解决方案:发送请求后,您必须关闭output流。 Alternatively detect the end of the request on server side without waiting for the "end of stream" limit. 或者,在服务器端检测请求的结束,而无需等待“流的结束”限制。

In your server code: 在您的服务器代码中:

            while ((line = in.readLine()) != null) {
                filedata.add(line);
                System.out.println(line);
            }
             writeToFile(filename, filedata); // getting to this line?

Your server is never getting to the writeToFile line, because the socket connection is still open, and it's still in the while loop. 您的服务器永远不会进入writeToFile行,因为套接字连接仍处于打开状态,并且仍处于while循环中。 As a solution, use DataFetcher 作为解决方案,请使用DataFetcher

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

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