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 7658385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:11:41+00:00 2026-05-31T13:11:41+00:00

The client import java.io.*; import java.net.*; import java.util.Scanner; public class HTCPCPClient { public static

  • 0

The client

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class HTCPCPClient {

    public static void main(String[] args) throws IOException {

        HTCPCPClient client = new HTCPCPClient();
        System.out.println("WELCOME TO THE COFFEE POT APPLICATION!");
        client.startClient();
    }

    private void startClient() throws IOException {
        final String HOST = "localhost";
        final int PORT_NUMBER = 4444;
        Socket clientSocket = null;
        PrintWriter outToServer = null;
        BufferedReader in = null;
        String serverSentence = null;
        String clientSentence = null;
        BufferedReader inFromServer = null;

        // create new socket
        clientSocket = new Socket(HOST, PORT_NUMBER);
        outToServer = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        do {  // wait for 'QUIT'              
            // Create input stream
            inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

             kbd = new Scanner(System.in);
             clientSentence = null;
             kbdInput = null;

                System.out.println("Enter Method ( e.g. BREW )");
                // next line of kbdInput from keybd.

                kbdInput = kbd.nextLine().trim();

                clientSentence = kbdInput + " coffee://127.0.0.1/pot-1 HTCPCP-new Accept-Additions: ";
                clientSentence = clientSentence + "\nstart\n@@";     

            // Send clientSentence to server
            outToServer.println(clientSentence);
            outToServer.flush();

            System.out.println("\nMESSAGE FROM SERVER:");

            do {
                serverSentence = inFromServer.readLine();
                System.out.println("\t" + serverSentence);

                if (serverSentence.equals("@@") == true) {
                    break;
                }
            } while (true);
            // read and print message from server

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

        // close connections 
        outToServer.close();
        in.close();
        inFromServer.close();
        clientSocket.close();
    } 

}

Server Thread

import java.io.*;
import java.net.*;

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();

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


                    if (clientSentence.contains("BREW")) {
                        outToClient.println("Message: " + clientSentence);
                        outToClient.println("HTCPCP-new 200 OK BREW START command completed.");
                        outToClient.println("Content-length:  " + clientSentence.length()); 
                        outToClient.println("@@");
                        outToClient.flush();
                    } else {
                        outToClient.println("Message: " + clientSentence);
                        outToClient.println("HTCPCP-new 400 Bad Request.");
                        outToClient.println("Content-length:  " + clientSentence.length()); 
                        outToClient.println("@@");
                        outToClient.flush();
                    }


                } // 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

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:  23
    @@

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: start
    HTCPCP-new 400 Bad Request.
    Content-length:  5
    @@

Select an option:
1. Brew
2. Quit

Notice that the messages from the server are different despite the same URL being entered.

Any ideas where I’m going wrong?

  • 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-31T13:11:42+00:00Added an answer on May 31, 2026 at 1:11 pm

    In your server, you’ve got this on every iteration of your loop:

    if (clientSentence.contains("BREW")) {
        outToClient.println("Message: " + clientSentence);
        outToClient.println("HTCPCP-new 200 OK BREW START command completed.");
        outToClient.println("Content-length:  " + clientSentence.length()); 
        outToClient.println("@@");
        outToClient.flush();
    } else {
        outToClient.println("Message: " + clientSentence);
        outToClient.println("HTCPCP-new 400 Bad Request.");
        outToClient.println("Content-length:  " + clientSentence.length()); 
        outToClient.println("@@");
        outToClient.flush();
    }
    

    So the server will read “BREW” (etc), then spit out all that output, ending with @@. Your client displays all of that, and then asks for the next input… but the server won’t have finished sending, because it will have read the next line of input, which is “start”. It then prints out that second response, even though it’s still reading the first request.

    I suggest you finish reading the request then write out a response…

    Note that your input loop should also have an exclusive upper bound, too:

    for (int i = 0; i <= clientInputLine.length; i++) {
        ...
        // This will blow up if i == clientInputLine.length
        clientInputLine[i].append(clientSentence);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my code on the client side: import java.io.*; import java.net.*; public class
In my GWT app I have the following model class: import com.google.gwt.user.client.rpc.IsSerializable; public class
I wrote a server-client communication program and it worked well. Client module import java.io.*;
I'm using this: from twisted.web.client import getPage df = getPage(url) # there is some
I used this code in my Android application: import org.apache.http.client.methods.HttpPost; ... HttpClient httpclient =
I'm working on an IMAP client using Ruby and Rails. I can successfully import
I am writing a auction server and client and using a class called BidHandler
I am creating a SSL Server and Client in Java. The point of the
I have a Java client that connects to a C++ server using TCP Sockets
Client has sendpoints() method which is called by some other class that I did

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.