Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7668869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:28:00+00:00 2026-05-31T15:28:00+00:00

The user selects an option, in this case option 1 – Brew. They then

  • 0

The user selects an option, in this case option 1 – Brew. They then enter the URL, in this case BREW. I’m only checking the word BREW at the moment so coffee://127… is irrelevant at this stage.

A message response is received back from the server. Notice the 200 OK being sent in the first server response message.

The application continues to loop where the user enters to URL again, again it’s BREW.

The problem this time is that a 400 Bad Request message is returned. As the URL is correct, they should receive 200 OK like the first pass around the application.

Client Console:

WELCOME TO THE COFFEE POT APPLICATION!

Select an option:
1. Brew
2. Quit
1
Enter URL (e.g. BREW coffee://127.0.0.1/pot-1 HTCPCP-new )
BREW

MESSAGE FROM SERVER:
    Message: BREW Accept-Additions: *, *, *, *, *, *, *, 
    HTCPCP-new 200 OK BREW START command completed.
    Content-length:  44
    Content-type: application/octet-stream
    @@

Select an option:
1. Brew
2. Quit
1
Enter URL (e.g. BREW coffee://127.0.0.1/pot-1 HTCPCP-new )
BREW

MESSAGE FROM SERVER:
    Message: BREW Accept-Additions: *, *, *, *, *, *, *,  
    HTCPCP-new 400 Bad Request.
    Content-length:  5
    Content-type: application/octet-stream
    @@

Select an option:
1. Brew
2. Quit

Client Thread Handler:

import java.io.*;
import java.net.*;
import javax.activation.MimetypesFileTypeMap;

public class HTCPCPClientWorker extends Thread {

    Socket cwsocket = null;

    public HTCPCPClientWorker(Socket cwsocket) {
        super("ClientWorker");
        this.cwsocket = cwsocket;
    }

    @Override
    public void run() {

        String clientSentence = null;
        BufferedReader inFromClient = null;
        PrintWriter outToClient = null;

        try {
            inFromClient = new BufferedReader(new InputStreamReader(cwsocket.getInputStream()));
            outToClient = new PrintWriter(cwsocket.getOutputStream(), true);
        } catch (IOException ex) {
            System.err.println("Cannot create streams");
        }

        try {

            do { // end when client says QUIT

                StringBuffer clientInputLine[] = new StringBuffer[3];

                clientInputLine[0] = new StringBuffer();
                clientInputLine[1] = new StringBuffer();

                // Get next message from client 
                for (int i = 0; i <= clientInputLine.length; i++) {

                    // read input line from BufferedReader 
                    clientSentence = inFromClient.readLine();

                    if (clientSentence.contains("BREW")) {
                        outToClient.println("Message: " + clientSentence);
                        outToClient.println("HTCPCP-new 200 OK BREW START command completed.");
                        outToClient.println("Content-length:  " + clientSentence.length()); // length needs to correspond to above line 
                        outToClient.println("Content-type: " + new MimetypesFileTypeMap().getContentType(clientSentence));
                        outToClient.println("@@");
                        outToClient.flush();
                    } else {
                        outToClient.println("Message: " + clientSentence);
                        outToClient.println("HTCPCP-new 400 Bad Request.");
                        outToClient.println("Content-length:  " + clientSentence.length()); // length needs to correspond to above line 
                        outToClient.println("Content-type: " + new MimetypesFileTypeMap().getContentType(clientSentence));
                        outToClient.println("@@");
                        outToClient.flush();
                    }

                    // wait for EOF = @@ 
                    System.out.println("\tInput: " + clientSentence);
                    if (clientSentence.equals("@@") == true) {
                        break;
                    }
                    clientInputLine[i].append(clientSentence);
                } // end for loop


            } while (!clientSentence.contains("QUIT"));

            outToClient.println("GOODBYE!");
            outToClient.flush();

            System.out.println("\tClient has disconnected.");
            cwsocket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    } // end run

} // end HTCPCPClientWorker.java

I thought the issue would be rectified by recreating the StringBuffer on each pass but this doesn’t appear to be the case.

3rd pass and onwards appear to function correctly.

Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:28:02+00:00Added an answer on May 31, 2026 at 3:28 pm

    I believe you should recreate the BufferedReader (inFromClient) each time you want to read from the socket. The BufferedReader gets empty after you use readLine, so the next time you put input on the socket he wouldn’t read it.

    There’s more information about reading and writing to sockets here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to do something when a user selects an option from a select
I want to reset a file upload field when the user selects another option.
I need to prompt an alert message when a user selects a particular option
Suppose a user selects a file in a dialogue box, and the app then
there is a dropdown with 5 options. Currently,option 2 is selected.The user selects option
I have form, where user selects Airport and then can select one of airport
I am looking to trigger a while loop, but only after the user selects
I have some options for pricing using radio buttons. When a user selects the
I have a selectbox with three options. When a user selects one of the
When a user selects a file in a web page I want to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.