Bufferedreader永远不会为null,而循环不会终止

时间:2015-02-20 04:22:57

标签: java sockets null bufferedreader

我正在创建一个程序,其中客户端使用代理来请求服务器从URL检索内容,例如,客户端键入" http://www.google.ca",他们从该网页获取HTML代码。我让它工作一次,但我想多次工作。我尝试使用循环,但它们永远不会终止,bufferedreader对于Proxy或Client类似乎永远不会为null(尽管Server类工作正常)。任何帮助将不胜感激。

这是我的代码

Client.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();
    }

}

Proxy.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();
    }

}

Server.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();
    }

}

1 个答案:

答案 0 :(得分:1)

  

bufferedreader似乎永远不会= null

你的问题有点不精确。你的意思是BufferedReader.readLine()永远不会返回null.这只会在对等关闭连接时发生。如果他从未关闭它,readLine()将不会返回null。

如果您正在编写HTTP代理,则需要仔细查看RFC 2616,特别是有关Content-length标头,Connection: close标头和分块传输编码的要求。只是将响应流读到它的结尾通常是不够的。