gpt4 book ai didi

java - Bufferedreader 永远不会为空,while 循环不会终止

转载 作者:太空宇宙 更新时间:2023-11-04 06:11:49 28 4
gpt4 key购买 nike

我正在制作一个程序,客户端使用代理要求服务器从 URL 检索内容,例如,客户端输入“http://www.google.ca ”,然后他们从该网页获取 html 代码。我让它工作一次,但我希望它工作多次。我尝试使用循环,但它们永远不会终止,对于代理或客户端类,bufferedreader似乎永远不会= null(尽管服务器类工作正常)。任何帮助将不胜感激。

这是我的代码

客户端.java

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

public class Client {

public static void main(String[] args) throws Exception {
Client serv = new Client();
serv.run();
}

public void run() throws Exception {
Socket sock = new Socket("localhost", 7777); //connects to Proxy

PrintStream ps = new PrintStream(sock.getOutputStream()); //output stream to Proxy

InputStreamReader ir = new InputStreamReader(sock.getInputStream()); //input stream from Proxy
BufferedReader br = new BufferedReader(ir);

Scanner scanner = new Scanner(System.in); //take input from keyboard

//user types in an URL, outputs content of URL
for(int i = 0; i < 100; i++) {
ps.println(scanner.nextLine());
String inputLine;
while((inputLine = br.readLine()) != null) {
System.out.println(inputLine);
}
System.out.println("DONE");
}
sock.close();
scanner.close();
}

}

代理.java

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

public class Proxy {

public static void main (String[] args) throws Exception {
Proxy serv = new Proxy();
serv.run();
}

public void run() throws Exception {
ServerSocket ss = new ServerSocket(7777);
Socket sock = ss.accept();

//input and output streams to and from Client
InputStreamReader ir = new InputStreamReader(sock.getInputStream());
BufferedReader br = new BufferedReader(ir);
PrintStream psToClient = new PrintStream(sock.getOutputStream());


//input and output streams to and from Server
Socket sck = new Socket("localhost", 8888);
PrintStream ps = new PrintStream(sck.getOutputStream());
InputStreamReader irFromServer = new InputStreamReader(sck.getInputStream());
BufferedReader brFromServer = new BufferedReader(irFromServer);

//passes message from Client to Server, passes URL content from Server back to Client
for(int i = 0; i < 100; i++) {
String message = br.readLine();
ps.println(message);
System.out.println("Client wants me to tell Server he wants the content of: " + message);
String inputLine;
while((inputLine = brFromServer.readLine()) != null) {
psToClient.println(inputLine);
//psToClient.flush();
System.out.println(inputLine);
}
System.out.println("DONE");
}

sock.close();
ss.close();
sck.close();
}

}

服务器.java

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

public class Server {

public static void main (String[] args) throws Exception {
Server serv = new Server();
serv.run();
}

public void run() throws Exception {
ServerSocket ss = new ServerSocket(8888);
Socket sock = ss.accept();

//input and output streams to and from Proxy
InputStreamReader ir = new InputStreamReader(sock.getInputStream());
BufferedReader br = new BufferedReader(ir);
PrintStream psToProxy = new PrintStream(sock.getOutputStream());

//retrieves content from requested URL and sends back to Proxy
for(int i = 0; i < 100; i++) {
String request = br.readLine();
System.out.println("Proxy told me Client wants the content from: " + request);

//makes URL object using String value from Client
URL url = new URL(request);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

//sends URL content back to Client, via Proxy
String inputLine;
while ((inputLine = in.readLine()) != null) {
psToProxy.println(inputLine);
//psToProxy.flush();
System.out.println(inputLine);
}
System.out.println("FINISHED SENDING CONTENT");
in.close();
}

sock.close();
ss.close();
}

}

最佳答案

bufferedreader never seems to = null

你的问题有点不精确。您的意思是 BufferedReader.readLine() 永远不会返回 null。 仅当对等方关闭连接时才会发生这种情况。如果他从不关闭它,readLine() 将不会返回 null。

如果您正在编写 HTTP 代理,则需要仔细阅读 RFC 2616,特别是有关 Content-length header 、Connection: close header 和分块传输编码的要求。仅仅读取响应流的末尾通常是不够的。

关于java - Bufferedreader 永远不会为空,while 循环不会终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28621592/

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