简体   繁体   中英

Bufferedreader never null, while loop won't terminate

I'm making a program where a client uses a proxy to ask a server to retrieve content from an URL, so for example, the client types in " http://www.google.ca ", and they get the html code from that webpage. I got it to work once, but I want to it work multiple times. I tried using loops but they never terminate, bufferedreader never seems to = null for the Proxy or Client class (the Server class works fine though). Any help would be greatly appreciated.

Here is my code

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

}

bufferedreader never seems to = null

Your question is a little imprecise. What you mean is that BufferedReader.readLine() never returns null. That only happens when the peer closes the connection. If he never closes it, readLine() won't return null.

If you're writing an HTTP proxy you need to have a good look at RFC 2616, and specifically the requirements about the Content-length header, the Connection: close header, and chunked transfer encoding. Just reading the response stream to its end is in general not sufficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM