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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:24:42+00:00 2026-05-26T06:24:42+00:00

I am learning android but I can’t get past the InputStream.read(). This is just

  • 0

I am learning android but I can’t get past the InputStream.read().
This is just a socket test – the server sends back two bytes when it receives a connection and I know that this working fine. All I want to do is read these values. The b = data.read reads both values in turn but then hangs, it never returns the -1 value which is what expect it to. Also it does not throw an exception.

Any ideas?
Thanks.

protected void startLongRunningOperation() {

    // Fire off a thread to do some work that we shouldn't do directly in the UI thread
    Thread t = new Thread() {
        public void run() {
            try {
                Log.d("Socket", "try connect ");
                Socket sock = new Socket("192.168.0.12", 5001);
                Log.d("socket", "connected");
                InputStream data = sock.getInputStream();

                int b = 0;
                while (b != -1) {
                    b = data.read();

                }


                data.close();
            } catch (Exception e) {
                Log.d("Socket", e.toString());
            }
        }
    };
    t.start();
}
  • 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-26T06:24:42+00:00Added an answer on May 26, 2026 at 6:24 am

    Reaching the end of the stream is a special state. It doesn’t happen just because there is nothing left to read. If the stream is still open, but there’s nothing to be read, it will “hang” (or block) as you’ve noticed until a byte comes across.

    To do what you want, the server either needs to close/end the stream, or you need to use:

    while (data.available() > 0) {
      ..
    

    When the number of available bytes is zero, there’s nothing sitting in the stream buffer to be read.

    On the other hand, if you know that there should only ever be two bytes to read, and that’s the end of your data, then just read the two bytes and move on (i.e. don’t use a while loop). The reason to use a while loop here would only be if you weren’t sure how many total bytes to expect.

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

Sidebar

Related Questions

I'm familiar with C++ but never used Java. I've just started learning Android development.
I have just started learning android. And i don't know How can I change
I am just learning about Android Development so excuse me if this is a
I've been learning android development (in Eclipse) the past week. I'm following online courses
I am working on learning Android. From the documents I have read so far
I'm interested in learning Android development.... but if I install the latest 2.0 SDK,
I've been learning android development and been trying to get the app on my
I've been learning android development and been trying to get the app on my
I'm learning to develop applications for Android but I need to know the XML
I'm completely new to programming on the Android platform but I'm interested in learning

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.