虽然 Loop 无法正常工作

While Loop not working as it should be

客户端会请求一个文件,如果服务器中存在该文件,则服务器发送该文件并给出确认信息。所以我想使用主 while 循环获取输入,但它在第一次迭代后停止工作,

客户端

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class WebClient {

    public static void main(String[] args) throws Exception {

        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); 

        String req;

        System.out.println("Do you want to search? (Y/N): ");
        Scanner user_input = new Scanner(System.in);

        req = user_input.next();

        while (req.equals("Y")) {

            Socket clientSocket = new Socket("localhost", 2000);

            System.out.println("Enter the file name: ");

            String file = inFromUser.readLine();
            DataOutputStream serverOutput = new DataOutputStream(clientSocket.getOutputStream());

            serverOutput.writeBytes(file + '\n');  

            BufferedReader serverInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));  

             System.out.println("Text from the file: ");

            while (true) {
                String data = serverInput.readLine();
                if (data == null) {
                     break;
                }
                System.out.println(data);
            } 

            clientSocket.close();

            System.out.println("Do you want to search again? (Y/N): ");        
            req = user_input.next();


        }

    }

}

服务器端

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

public class WebServer {

  public static void main(String[] args) throws Exception
    {
        ServerSocket welcomeSocket = new ServerSocket(2000);
        while (true) 
        {
            Socket connectionSocket = welcomeSocket.accept();

            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            String file = inFromClient.readLine();

            System.out.println("Client Request: " + file); //Show The Client Request File

            String path = ("E://From Varsity//4.2//Lab//Network Programming//Java trying//New TCP-Client+Server//tcp")+ "/" + file ;
            File objfile = new File(path);

            DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            if (objfile.exists())
            {
               String readfile = rfile(path);
               outToClient.writeBytes("\n" +readfile + "200 ok \n"); // when exact file find
            }  
            else
            {
                outToClient.writeBytes("404 The Requested File not found \n"); // file not found
            }

        }
    }

    public static String rfile(String file_N) throws Exception
    {
        StringBuilder app = new StringBuilder();
        BufferedReader bufferR = new BufferedReader(new FileReader(file_N));
        try 
        {
            String line = bufferR.readLine(); // read file from buffer
            while (line != null) {
                app.append(line);  // append the line 
                app.append("\n");
                line = bufferR.readLine();
            }
        } 
        finally 
        {
           bufferR.close(); 
        }
        return app.toString();
    }
}

任何帮助将不胜感激,提前致谢

java.net.Socket 正在阻塞。它会阻塞直到它收到关闭(对 readLine() 的调用阻塞直到有更多数据可用)

3 个解决方案:

最简单:在写入后添加outToClient.close()。

非阻塞:使用 java.nio.SocketChannel/java.nio.ServerSocketChannel

线程化:每次 ServerSocket.accept() 使用来自 accept 的 Socket 对象触发时创建一个新线程。