gpt4 book ai didi

java - While 循环没有按应有的方式工作

转载 作者:行者123 更新时间:2023-12-02 05:03:43 25 4
gpt4 key购买 nike

客户端将请求一个文件,如果该文件存在于服务器中,则服务器发送该文件并给出确认消息。所以我想使用主 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

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

关于java - While 循环没有按应有的方式工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28001355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com