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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:19:08+00:00 2026-05-30T06:19:08+00:00

I am trying to deal with client disconnects in a simple java application that

  • 0

I am trying to deal with client disconnects in a simple java application that sends chat messages using a client and a server.

Here’s the problem

public void run() {
    try {
        System.out.println("Client Connected");
        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            System.out.println(inputLine);
            theServer.handle(inputLine, this);
        }
        out.close();
        in.close();
        socket.close();
        System.out.println("Client Disconnected");

    } catch (Exception e) {
        e.printStackTrace();
    }

}

With this code here, it runs the while loop while the input line is not null. If it is null then it will stop looping and close the in and out streams as well as the socket. Now, this is all working fine apart from the output in the console is this:

Client Connected
null
Client(/127.0.0.1): null
null
Client(/127.0.0.1): null
null
Client(/127.0.0.1): null
null
Client(/127.0.0.1): null
Client Disconnected

I can’t figure out why the loop would keep looping even when the input is null!! It seems to take a while to realise that it’s null before exiting the loop. I need it to exit straight away without a delay.

Any help would be appreciated,

shadrxninga

  • 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-30T06:19:09+00:00Added an answer on May 30, 2026 at 6:19 am

    The only explanations that I can think of are:

    • in.readLine() is returning the string "null" (possibly with trailing whitespace),
    • in refers to some custom stream or reader wrapper whose readLine() method is doing something weird, or
    • this is not the code that you are actually executing.

    You say:

    “It seems to take a while to realise that it’s null before exiting the loop.”

    This is a “voodoo programming” explanation. It cannot possibly be correct unless there’s a bug in your Java implementation. And that is extremely unlikely.

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

Sidebar

Related Questions

Alright so here is the deal. I am trying to make a application that's
I am trying to deal with an IE7 bug in my application. Here is
I'm trying to make a little console application that is able to deal with
I'm trying to write some simple chat client in javascript, but I need a
I am trying to create my own WebSocket Server with Java. When my client
I got this crazy problem I'm trying to deal with. I know that when
I'm trying to implement a simple web server on Linux that connects to the
I am trying to create a desktop application using eclipse-rcp. In that application, I
I've been trying to deal with some delimited text files that have non standard
I'm trying to read the transactional deal letter queue in MSMQ using C#.NET and

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.