开发者

Multiple println from server to client

I am currently having an issue with a Server - Client interaction.

I am needed to read multiple println that a server sends to a client, this works but after it has read the lines it doesn't seem to go back to waiting for the user of the client to enter a new command

This is the method that opens up the streams

private void openStreams() throws IO开发者_运维问答Exception
{
    final boolean AUTO_FLUSH = true;
    is = socket.getInputStream();
    os = socket.getOutputStream();
    fromServer = new BufferedReader(new InputStreamReader(is));
    toServer = new PrintWriter(os, AUTO_FLUSH);
}

This is the method that sends the request then reads them out

private void sendRequest() throws IOException
{
        String request;
        String reply;
        Scanner sc = new Scanner(System.in);

        request = sc.nextLine();
        while(!(request.equals(CLIENT_QUITTING)))
        {
            toServer.println(request);

            while((reply = fromServer.readLine()) != null)
            {
                System.out.println(reply);
            }

            request = sc.nextLine();
        }  
    }

It seem to be getting stuck on the inner while loop

Can anyone point me in the direction of where I am going wrong?


The receiving code somehow needs to know when to stop reading text, and when to expect the next command.

For example, the Simple Mail Transfer Protocol (SMTP) defines that the body of the mail consists of several lines, and the line .\r\n marks the end.

Another possibility is what HTTP does for chunked encoding (with bytes, but the concept is the same). It sends the body as a sequence of chunks. Each chunk consists of a length field and the data (which is length bytes long). In your case you would probably send the number of lines that the receiver may expect, and then the lines themselves.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜