Java BufferedWriter不刷新

Java BufferedWriter不刷新,java,http,sockets,client-server,printwriter,Java,Http,Sockets,Client Server,Printwriter,我有以下问题 try { clientInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); clientOutput = new BufferedWriter(new PrintWriter(clientSocket.getOutputStream()));

我有以下问题

            try 
            {
                clientInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                clientOutput = new BufferedWriter(new PrintWriter(clientSocket.getOutputStream()));

                while(true)
                { 
                    String clientRequest = "";
                    String tempStr = clientInput.readLine();

                    while(tempStr != null && !tempStr.equals("null"))
                    {
                        System.out.println(tempStr);
                        clientRequest += tempStr + " ";
                        tempStr = clientInput.readLine();
                    }

                    //Parse Request
                    ArrayList<String> tokenArray = parseRequest(clientRequest);

                    Calendar c = Calendar.getInstance();

                    switch(tokenArray.get(0))
                    {
                        case "GET": 
                        {
                            clientOutput.write("HTTP/1.1 200 OK\r\n");
                            clientOutput.write("Date: " + c.getDisplayName(0, Calendar.LONG, Locale.UK).toString() + "\r\n");
                            clientOutput.write("Server: Java HTTP Server v1.0\r\n");
                            clientOutput.flush();
                            break;
                            //Write File
                        }
                        default: 
                        {
                            clientOutput.write("500\r\n");
                            clientOutput.flush();
                        }
                    }
                } 
            }

服务器类:Strydom_A_201103578_P03

public class Strydom_A_201103578_P03
{
    Thread[] threadArray = new Thread[5];
    int ClientCount = 0;

    public Strydom_A_201103578_P03() throws ClientSizeExceededException 
    {
        ServerSocket httpServer = null;
    try 
    {
        httpServer = new ServerSocket(1337);
    } 
    catch (IOException ex) 
    {
        Logger.getLogger(Strydom_A_201103578_P03.class.getName()).log(Level.SEVERE, null, ex);
    }

    while(true)
    {
        try
        {
            //Wait for connection

            Socket clientSocket = httpServer.accept();

            if(ClientCount < 5)
            {
                threadArray[ClientCount] = new Thread(new clientHandler(clientSocket));
                threadArray[ClientCount].start();
                ClientCount++;
            }
            else
            {
                throw new ClientSizeExceededException();
            }

        }
        catch(IOException ex)
        {

        }
        finally
        {

        }
    }
}

class clientHandler implements Runnable
{
    Socket clientSocket;
    public clientHandler(Socket clientSocket) 
    {
        this.clientSocket = clientSocket;
    }

    @Override
    public void run() 
    {
        BufferedReader clientInput = null;
        BufferedWriter clientOutput = null;

            try 
            {
                clientInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                clientOutput = new BufferedWriter(new PrintWriter(clientSocket.getOutputStream()));

                clientOutput.write(" ");
                clientOutput.flush();

                while(true)
                { 
                    String clientRequest = "";
                    String tempStr = clientInput.readLine();

                    while(tempStr != null && !tempStr.equals("null"))
                    {
                        System.out.println(tempStr);
                        clientRequest += tempStr + " ";
                        tempStr = clientInput.readLine();
                    }

                    //Parse Request
                    ArrayList<String> tokenArray = parseRequest(clientRequest);

                    Calendar c = Calendar.getInstance();

                    switch(tokenArray.get(0))
                    {
                        case "GET": 
                        {
                            clientOutput.write("HTTP/1.1 200 OK\r\n");
                            clientOutput.write("Date: " + c.getDisplayName(0, Calendar.LONG, Locale.UK).toString() + "\r\n");
                            clientOutput.write("Server: Java HTTP Server v1.0\r\n");
                            clientOutput.flush();
                            break;
                            //Write File
                        }
                        default: 
                        {
                            clientOutput.write("500\r\n");
                            clientOutput.flush();
                        }
                    }
                } 
            }
            catch (IOException ex) 
            {
                Logger.getLogger(Strydom_A_201103578_P03.class.getName()).log(Level.SEVERE, null, ex);
            } 
            finally 
            {
                try 
                {
                    clientInput.close();
                    clientOutput.close();
                } 
                catch (IOException ex) 
                {
                    Logger.getLogger(Strydom_A_201103578_P03.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    }

    private ArrayList<String> parseRequest(String tempStr) 
    {
        StringTokenizer httpTokens = new StringTokenizer(tempStr, " ");
        ArrayList<String> tokens = new ArrayList<>();

        while(httpTokens.hasMoreTokens())
            tokens.add(httpTokens.nextToken());

        return tokens;
    }
}


public static void main(String[] args) throws ClientSizeExceededException 
{
    new Strydom_A_201103578_P03();
}
}

客户端类:TestClient


创建项目(或2)并运行文件

您需要更改内部while循环以查找客户端请求的结尾:

while(tempStr != null && !tempStr.equals("null"))
致:

客户端在发送请求后不会断开连接(导致null)。它将给您一个空白行,以表明其请求的结束

立即返回响应头的原因是有效的?也许客户端只是读取200并(最终)断开连接?因此,当您读取客户机的请求时,它将结束,并最终得到一个空值


编辑:


所以运行你的代码,对我来说很好。客户端和服务器都在发送和接收请求和响应。但是,服务器从不断开连接(客户端包含一个
连接:close
头),客户端继续在
readLine()
上阻塞。毫不奇怪,当我在服务器端设置连接后立即包含
write()
flush()
时,除了在客户端看到两次
HTTP/1.1 200 OK
之外,没有任何变化。可能您只需关闭
try/catch{}
末尾的
finally{}
块中的
clientSocket

您需要更改内部while循环以查找客户端请求的结尾:

while(tempStr != null && !tempStr.equals("null"))
致:

客户端在发送请求后不会断开连接(导致null)。它将给您一个空白行,以表明其请求的结束

立即返回响应头的原因是有效的?也许客户端只是读取200并(最终)断开连接?因此,当您读取客户机的请求时,它将结束,并最终得到一个空值


编辑:


所以运行你的代码,对我来说很好。客户端和服务器都在发送和接收请求和响应。但是,服务器从不断开连接(客户端包含一个
连接:close
头),客户端继续在
readLine()
上阻塞。毫不奇怪,当我在服务器端设置连接后立即包含
write()
flush()
时,除了在客户端看到两次
HTTP/1.1 200 OK
之外,没有任何变化。也许你所需要做的就是关闭
try/catch{}
末尾的
finally{}
块中的
clientSocket

只要这样做,它就会工作

添加
true
作为
PrintWriter


clientOutput=new BufferedWriter(new PrintWriter(clientSocket.getOutputStream(),true))

只要这样做,它就会工作

添加
true
作为
PrintWriter


clientOutput=new BufferedWriter(new PrintWriter(clientSocket.getOutputStream(),true))

因此,为了我现在结束的痛苦的未来——以下是我最终所做的

我将服务器和客户端读卡器从
BufferedReader/Writer
更改为
DataInputstream/OutputStream
。。。。现在它工作得很好-!谢谢大家


艾登

为了我现在结束的痛苦的未来——以下是我最终所做的

我将服务器和客户端读卡器从
BufferedReader/Writer
更改为
DataInputstream/OutputStream
。。。。现在它工作得很好-!谢谢大家


艾登

这里的问题是
PrintWriter
。它吞噬例外。将其更改为
OutputStreamWriter
。然后,您将看到任何正在被吞并的异常。一般来说,您应该避免通过网络使用
printWriter
PrintOutputStreams
。它们包含您需要了解的异常。

这里的问题是
PrintWriter
。它吞噬例外。将其更改为
OutputStreamWriter
。然后,您将看到任何正在被吞并的异常。一般来说,您应该避免通过网络使用
printWriter
PrintOutputStreams
。它们吞下了您需要了解的异常。

对我来说,它是有效的

***换行:换行对于浏览器解释标题的结尾和内容的开头很重要。如果没有它,刷新将不工作,响应也不会发送

out.write("GET HTTP/1.0\r\n")
out.write("Accept: text/plain, text/html, text/*\r\n")
out.write("\r\n") // THIS BREAK LINE ***
out.write("It Works")
out.flush()
out.close()
对我来说,这很有效

***换行:换行对于浏览器解释标题的结尾和内容的开头很重要。如果没有它,刷新将不工作,响应也不会发送

out.write("GET HTTP/1.0\r\n")
out.write("Accept: text/plain, text/html, text/*\r\n")
out.write("\r\n") // THIS BREAK LINE ***
out.write("It Works")
out.flush()
out.close()

我甚至怀疑你能走那么远。你确定你正在突破
while(tempStr!=null&&!tempStr.equals(“null”)
循环吗?…嗨,大卫。。。如前所述,一切正常,直到那里-我用调试器检查了代码-服务器正在编写complte头。。。而且确实达到了高潮……啊,好吧。我打赌客户端收到了服务器发送的所有数据,并且再次调用了
readLine
。(您尚未发送完整的HTTP响应。除了等待其余响应之外,您希望客户端做什么?)…这很公平-但为什么只有在while循环的顶部添加两行额外的代码时,这才起作用。。。。当然,它也应该“愚蠢地”——}再次调用
ReadLine
。。。。如果我错了,请纠正我你到底有什么问题?是客户被卡住了吗?还是服务器的问题?我怀疑你能做到这一点。你确定你正在突破
while(tempStr!=null&&!tempStr.equals(“null”)
循环吗?…嗨,大卫。。。如前所述,一切正常,直到那里-我用调试器检查了代码-服务器正在编写complte头。。。而且确实达到了高潮……啊,好吧。我打赌客户机收到了服务器发送的所有数据,只是调用了while(tempStr != null && !tempStr.equals("null"))
while(tempStr != null && !tempStr.equals("null") && !tempStr.equals(""))
out.write("GET HTTP/1.0\r\n")
out.write("Accept: text/plain, text/html, text/*\r\n")
out.write("\r\n") // THIS BREAK LINE ***
out.write("It Works")
out.flush()
out.close()