我的客户端是一个网络浏览器,并使用此 url 向 myserver 发送请求:
http://localhost
这是服务器端代码。问题出在类的run方法上ServingThread。
class ServingThread implements Runnable{
    private Socket socket ;
    public ServingThread(Socket socket){
        this.socket = socket ;
        System.out.println("Receives a new browser request from "
                      + socket + "\n\n");
    }
    public void run() {
        PrintWriter out = null ;
        try {
            String str = "" ;
            out = new PrintWriter( socket.getOutputStream() ) ;
            out.write("This a web-page.") ;
            // :-(
            out.flush() ;
            // :-(
            socket.close() ;
            System.out.println("Request successfully fulfilled.") ;
        } catch (IOException io) {
            System.out.println(io.getMessage());
        }
    }
}
我是否正在使用
out = new PrintWriter( socket.getOutputStream(), true ) ;
或者
out = new PrintWriter( socket.getOutputStream() ) ;
输出没有进入浏览器。仅当我使用流手动刷新时,输出才会进入浏览器
out.flush() ;
我的问题: new PrintWriter( socket.getOutputStream(), true )应该自动刷新输出缓冲区,但它没有这样做。为什么?