【问题标题】:Bufferedreader never null, while loop won't terminateBufferedreader 永远不会为空,while 循环不会终止
【发布时间】: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


    【解决方案1】:

    bufferedreader 似乎永远不会 = null

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-24
      • 2020-02-28
      • 2011-01-20
      • 2018-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多