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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:58:06+00:00 2026-06-04T06:58:06+00:00

I writing downloader app. I want to only use java Socket to request a

  • 0

I writing downloader app. I want to only use java Socket to request a file. So I write in my socket just as HTTP protocol rules. My app creates a connection and after reading headers, uses read() method of InputStream of my socket. Everything goes well. sometimes connection may lost. but I store which byte I was reading, so again it creates a new Socket with HTTP Ranged GET and continue its work. But when download is about to finish, I mean when less than 10 KB remains, All Connections will lost and it again(as scheduled) try to open new Socket and Continue its work. It completely reads headers of response but before reading any byte of body, read() method returns -1 and Again and Again it tries to Open a new Socket and read() remaining bytes, but the problem persists. The point is every time response headers can completely be read. and I see the Content-Length: field of response header is exactly the remaining bytes of file. I forgot to mention: my code has a problem because I check many file from many servers and result is the same. here is the Code:

// Some fields:
int state;
long start, current, end;

// in a thread:
while (state != FINISHED) {
    if (state == DOWNLOADING) {
        try {
            // fill a new socket with Ranged GET [current, end]
            Socket s = initConnection();
            InputStream in = s.getInputStream();
            int readNo = 0;
            FileOutputStream out = getTempFile();
            byte[] buffer = new byte[1024];
            // read response headers successfully and prints them, request range is OK. a sample of its print is at the end of page
            readHeaders(in);
            while (state == DOWNLOADING && (readNo = in.read(buffer)) != -1) {                      
                current += readNo;
                out.write(buffer, 0, readNo);
            }
            if (readNo == -1) {
                // at nearly end of download always print this and values never changes, where usually they have 3000 byte difference
                System.out.println("**************> (" + current + " - " + end + ")");
            }
            if (currentByte == endByte) {
                state = FINISHED;
                //mergeParts();
                // code never reaches here
                dlInfo.checkAllPartsFinished();
            }
            out.flush();
            out.close();
            s.close();
        } catch (Exception e) {
            e.printStackTrace();
            state = ERROR;
            error = e.getMessage();
            errorRetry++;
        }
    } else if (state == PAUSED) {
        // ...
    } else ...
    }
}

a sample of response header at the end of file where nothing changes:

HTTP/1.1 206 Partial Content
Date: Mon, 21 May 2012 14:34:27 GMT
Server: Apache
Last-Modified: Sat, 21 Apr 2012 02:16:20 GMT
ETag: "4006d32e-f691e0-4be26fda00500"
Accept-Ranges: bytes
Content-Length: 7859
Content-Range: bytes 2012041-2019899/16159200
Connection: close
Content-Type: application/octet-stream

**************> (2012041 - 2019899)

I don’t know what problem is but whatever it is, it occurs at nearly the end of stream.
I’m completely confused, after many hours spending time. I would be grateful for Any help!

thanx

  • 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-04T06:58:07+00:00Added an answer on June 4, 2026 at 6:58 am

    Are you layering a Buffered Reader/Stream on top of the InputStream in your readHeaders() method? my guess is that you are doing this, and that this buffered stream is reading more of the InputStream than you expect (since it is buffered). these bytes are then lost when you return from the readHeaders() method.

    UPDATE:

    just saw your last comment. that is exactly your problem. the BufferedReader is consuming part of the body bytes.

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

Sidebar

Related Questions

I'm writing my first iPhone app. I want to use some methods from the
I'm writing a JNI application and I want the app to download the correct
I'm writing a small application in CakePHP 2.1, and I want to use Nick
I'm writing a simple downloader app which has two activities: SearchActivity: shows a list
I am writing an app that downloads a wav file from a server and
I am writing an app, where a socket is connecting to a host and
I am writing an iPad app that downloads a rather large .csv file and
I'm writing an iPhone app which downloads a tar-gzipped file from a Webserver, then
I'm writing app for android device, which is simple download manager. I want to
I'm writing an app, part of which allows the user stream/play videos. I want

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.