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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:18:03+00:00 2026-06-02T03:18:03+00:00

I have a BufferedReader, when I try to read it, it just hangs and

  • 0

I have a BufferedReader, when I try to read it, it just hangs and doesn’t do anything, am I doing this right? I am using this in an AsyncTask.

  • Edit: I have a tablet connected to the Wi-Fi, this connects to my computer which is broadcasting on 172.20.104.203 on port 5334, I can see when the thread starts, but nothing after that.

Here my code:

try {
       final BufferedReader in = new BufferedReader(
       new InputStreamReader(socket.getInputStream()));       
       String line = null;
       while ((line = in.readLine()) != null) {
          final String msg;
          msg = (line);
          Log.d("DeviceActivity", msg);
       }
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("ClientAcivtity: Exception",
        String.valueOf(e));
    }
  • EDIT
    I have all the right permissions or anything, I was doing this outside a AsyncTask and it worked perfectly, moved it because I didn’t want it in the main thread.

–Edit , here is the full code.

public class NetworkTask extends AsyncTask<Void, byte[], Boolean> {
        Socket nsocket; // Network Socket
        InputStream nis; // Network Input Stream
        OutputStream nos; // Network Output Stream
        private Handler handler = new Handler();

        Boolean connected = false;

        public static final int PORT = 5334;
        public String SERVERIP = "172.20.104.203";

        Socket socket;

        @Override
        protected void onPreExecute() {
            Log.i("AsyncTask", "onPreExecute");
            InetAddress serverAddr;
            try {
                serverAddr = InetAddress.getByName(SERVERIP);
                socket = new Socket(serverAddr, PORT);
                connected = true;
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.e("ClientAcivtity: Exception", String.valueOf(e));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.e("ClientAcivtity: Exception", String.valueOf(e));
            }
        }

        @Override
        protected Boolean doInBackground(Void... params) { // This runs on a
                                                            // different thread
            boolean result = false;

            try {

                Log.d("ClientActivity", "C: Connecting...");
                if (socket != null) {
                    int cont = 1;
                    while (cont == 1) {
                        try {
                            Log.d("ClientActivity", "C: Sending command.");
                            PrintWriter out = new PrintWriter(
                                    new BufferedWriter(new OutputStreamWriter(
                                            socket.getOutputStream())), true);
                            // where you issue the commands
                            out.println("getPos");
                            Log.d("ClientActivity", "C: Sent " + "getPos");

                        } catch (Exception e) {
                            Log.e("ClientAcivtity: Exception",
                                    String.valueOf(e));
                        }
                        try {
                            final BufferedReader in = new BufferedReader(
                                    new InputStreamReader(
                                            socket.getInputStream()));
                            String line = null;
                            while ((line = in.readLine()) != null) {
                                final String msg;
                                msg = (line);
                                Log.d("DeviceActivity", msg);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            Log.e("ClientAcivtity: Exception",
                                    String.valueOf(e));
                        }
                        cont--;
                    }

                    Log.d("ClientActivity", "C: Closed.");
                }
            } catch (Exception e) {
                Log.e("ClientAcivtity: Exception", String.valueOf(e));
            }

            return result;
        }

        @Override
        protected void onProgressUpdate(byte[]... values) {
            if (values.length > 0) {
                Log.i("AsyncTask", "onProgressUpdate: " + values[0].length
                        + " bytes received.");
            }
        }

        @Override
        protected void onCancelled() {
            Log.i("AsyncTask", "Cancelled.");
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if (socket != null) {
                if (connected) {
                    if (result) {
                        Log.i("AsyncTask",
                                "onPostExecute: Completed with an Error.");
                        try {
                            socket.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    } else {
                        Log.i("AsyncTask", "onPostExecute: Completed.");
                        try {
                            socket.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
  • 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-02T03:18:05+00:00Added an answer on June 2, 2026 at 3:18 am

    My guess is that when you write out the command “getPos” the underlying BufferedWriter is not actually sending the data out on the line (you should verify this with tcpdump/wireshark). If this is the case, the server doesn’t responsed to the readLine(), since it never got a command. To verify this claim, add out.flush(); after out.println("getPos");

    Really, tcpdump will probably give you a better answer then anyone on the forums.

    Also see http://developer.android.com/reference/java/io/BufferedWriter.html

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

Sidebar

Related Questions

Hi I have been using the Buffered Reader to try to read a text
I have such code to read a text file using BufferedReader : BufferedReader reader=null;
I have 2 sockets and I am using BufferedReader around it's InputStreams. What I
I have some codes like this: PrintWriter pw = new PrintWriter(new BufferedReader(....)); for(int i=0;
I have this code: ServerSocket serverSideSocket = new ServerSocket(1234); serverSideSocket.accept(); BufferedReader in = new
I'm reading a file using bufferedreader, so lets say i have line = br.readLine();
I will try to keep this as simple as possible. Basically I have a
So I have the following code where I should read a Text File (This
I have a problem with writting this program I need to read a csv
I have to read a file using servlet.here is the code iam using.but file

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.