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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:54:09+00:00 2026-05-30T22:54:09+00:00

I am trying to develop a class that allows me to run a socket

  • 0

I am trying to develop a class that allows me to run a socket on a thread, and that at any time allows me to send through it data, as well as receive a notifications when data arrives. It should assume no things such as only receiving a message after it has sent a message first, etc.
By some reason, the following code is printing the response for only the first request:

public static void main(String[] args) throws IOException {
    TCPClient client = new TCPClient(new TCPClientObserver());
    client.connect("www.microsoft.com", 80);

    sleep(1000);
    client.send("HTTP GET");

    sleep(5000);
    client.send("XYZ");
}

printing

echo: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
echo: <HTML><HEAD><TITLE>Bad Request</TITLE>
echo: <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
echo: <BODY><h2>Bad Request - Invalid URL</h2>
echo: <hr><p>HTTP Error 400. The request URL is invalid.</p>
echo: </BODY></HTML>

Here is the core logic of the socket:

        echoSocket = new Socket("www.microsoft.com", 80);
        out = new PrintWriter(echoSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(
                echoSocket.getInputStream()));

        while (true) {
            String response = in.readLine();
            if (response != null)
                System.out.println("echo: " + response);
        }

I guess the problem lies in my loop?

The full test code of my app can be seen here:
http://codepad.org/bmHwct35

Thanks

  • 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-30T22:54:10+00:00Added an answer on May 30, 2026 at 10:54 pm

    Assuming the HTTP 400 is what you expected, you need to break out of your loop if readLine() returns null. This is usually written like this:

    while ((line = in.readLine()) != null)
    {
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to develop a dynamic library in C++ that can be run-time
I'm trying to develop a class that supports an asynchronus method invocation. This is
I'm trying to develop a basic GPA Calculator. I have an AllCourses class that
I'm trying to develop an application that will simulate a route through Google's Navigation
I'm trying to develop a program that takes requests for data which is stored
I'm trying to develop a small Stripes project that allows the user to uoload
I have been trying to develop a CustomButton class that extends MovieClip, however I
I'm trying to develop a pretty simple (for now) wrapper class around int ,
I'm trying to develop a portable J2ME appli, but Netbeans do a class preverification
Im trying to develop my first ASP.NET MVC web app and have run into

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.