【发布时间】:2015-04-21 16:42:07
【问题描述】:
我正在制作一个程序,其中客户端使用代理请求服务器从 URL 检索内容,例如,客户端键入“http://www.google.ca”,然后他们从该网页获取 html 代码。我让它工作一次,但我想它工作多次。我尝试使用循环,但它们永远不会终止,对于 Proxy 或 Client 类,bufferedreader 似乎永远不会 = null (尽管 Server 类工作正常)。任何帮助将不胜感激。
这是我的代码
客户端.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();
}
}
【问题讨论】:
标签: java sockets null bufferedreader