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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:24:13+00:00 2026-05-22T14:24:13+00:00

I have this client, the server information is not important. The output of this

  • 0

I have this client, the server information is not important. The output of this code is very random.

class Client {
    public static void main(String args[]) throws Exception

    {
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        Socket clientSocket = new Socket("127.0.0.1", 10004);//this will become the addr of the server you want to input.
        InetAddress host = clientSocket.getInetAddress();
        //      System.out.println(host);

        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        boolean exit = false;

        while (!exit) {
            while (inFromServer.ready()) {
                System.out.println(inFromServer.readLine());
            }
            String sentence = inFromUser.readLine();
            outToServer.writeBytes(sentence + "\n");
        }
        clientSocket.close();
    }
}

If I run this in debug mode, it has always the correct result. E.g.

  • please insert password
  • the user types pass
  • pass correct
  • please type command
  • and you type command
  • etc
  • etc

When it’s not in debug mode, all goes wrong. I don’t even get the initial request from server. What is going on? I think the read line might be executed too fast?

  • 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-22T14:24:14+00:00Added an answer on May 22, 2026 at 2:24 pm

    in.ready() does not wait for any data to be available. It the server hasn’t sent the data yet when you client reads that line, you’re going to skip the readLine() completely.

    Just remove that while and do a plain readLine().

    If there are phases where you need to wait for multiple lines from the server, you’ll need to implement more logic. Usually, the server will send an “end of message” marker to signify to the client that it is done. For example, when the server is done, it could send the message “END-OF-MESSAGE”. In the client code, you would do:

    boolean serverDone = false;
    while (!serverDone) {
      String message = in.readLine();
      if (message == null) {
        // handle this problem: the server has closed the connection
        serverDone = true; // or return, or throw
      } else if ("END-OF-MESSAGE".equals(message)) {
        serverDone = true;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this client/server application, and the client application sometimes completely freezes when i
I have created my web socket server and client using this simple tutorial here
i have data structure i am passing this from server to client using data
Hi I have an application that operations like this.. Client <----> Server <----> Monitor
I have this php code $jsonArray = array(); $sql = SELECT ID,CLIENT FROM PLD_SERVERS;
Ok, I have developed this WinForms client, which interacts with a server (ASPX Application)
I have functioning RMI client-server system. The server's host information is known to the
I have programed a server-client application as a RPC (low-level). This is my server
we have this scenario: A server which contains needed data and client component which
We have an existing API where a client asks our server for information that

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.