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

  • Home
  • SEARCH
  • 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 8738901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:49:59+00:00 2026-06-13T10:49:59+00:00

Is there any way to make the method readline() (from bufferedReader) block until it

  • 0

Is there any way to make the method readline() (from bufferedReader) block until it has in fact something to read??

I’m creating an client/server application socketBased and I have this

The Server reads and then writes. The Client writes and then reads.. this is the communication between client/server (based on sockets)

The only problem is that on the server I’m reading with bufferedReader.readLine() which isn’t a blocking method. I have already tried replacing the bufferedReader.readLine() by dataInputstream().read() (handle all the byte stuffs and getting out when -1 is received) and it isn’t working either.

The problem in all this is that the server is expecting to read but do not blocks. the client is only going to write to the server (through a socket) when the user press a button (so the server must be waiting for reading and it is not)

On the server side i have this:

    BufferedReader READ = new BufferedReader(new InputStreamReader(skt.getInputStream()));
        BufferedWriter WRITE = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
        FileInputStream fstream = new FileInputStream(file);
        DataInputStream in = new DataInputStream(fstream);

        boolean FLAG = true;

        String s = new String("#$");

        while (FLAG) {


            if (skt == null)
                FLAG = false;

            // READING FROM THE CLIENT
            while (!READ.ready()){System.out.println("Blocking")};

            while ((s = READ.readLine()) != null) {
                System.out.println("RECEIVING: " + s);
                System.out.println("$$$");
            }

            System.out.print("hi");
            // READING FROM THE FILE AND SENDING TO THE CLIENT
            //WRITTING TO THE CLIENT
            while ((strLine = in.readLine()) != null) {
                System.out.println("SENDING: " + strLine);
                strLine += "\n";
                WRITE.write(strLine);
                WRITE.flush();
                strLine = new String();
            }
        }

On the Client side i have this:

    //READING FROM THE FILE AND SENDING TO THE SERVER
                 while((strLine  = in.readLine()) != null)  
                 {  

                        strLine += "\n";


                        soma+=strLine.length();
                        WRITE.write(strLine);
                        WRITE.flush();

                        TextArea1.append(strLine);
                        ProgressBar.setValue(soma);
                        ProgressBar.repaint();  

                    };          
                    in.close();


                    ProgressBar.setValue(0);
                    soma=0;

                    //READING FROM THE SERVER
                    while((strLine=READ.readLine()) != null)
                    {

                        strLine+="\n";
                        soma+=strLine.length();

                        TextArea2.append(strLine);
                        strLine = new String();
                        ProgressBar.setValue(soma);
                        ProgressBar.repaint();  
                    }

                    in.close();
                    fstream.close();
                    WRITE.close();
                    READ.close();

                    Skt.close();

NOTE: Skt is a socket connecting client to server.

the problem here is that. When the client connects to the server, the server expects to read from the socket right after the connection is established.. however the code on the client (writing to the server) is only expected to run when the user presses a button.. So, the server must be blocked until the client in fact writes something to the socket which is not (the server is not blocking). :-/ hope to make my self more clear this time.. Sorry

  • 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-06-13T10:50:00+00:00Added an answer on June 13, 2026 at 10:50 am

    BufferedReader.readLine() always blocks until it finds '\n'.

    If you can give examples of data you’re sending around, I might help you further.

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

Sidebar

Related Questions

Is there any way to make a DataTemplate reference itself just from XAML? In
Is there any way to make the program sleep until an event occurs, but
is there Any Way To Make my Method Take many input variable but with
is there any way I can make the encodeURIComponent method ignore certain characters for
In PHP, is there any way to make class method automatically call some other
Since, an interface doesn't encompass a static method, is there any way to make
Is there any simple way to make a method that gets called whenever the
Is there any elegant way to make Java method located within parent class return
Is there any way to make the text align left and justify as same
Is there any way to make the form semi-transparent while it is being moved

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.