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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:27:33+00:00 2026-05-19T11:27:33+00:00

I’m writing a simple download accelerator. The problem is I can send and receive

  • 0

I’m writing a simple download accelerator. The problem is I can send and receive messages once. The next time I try to send and receive message, I get no response froms server. I’m not even sure if I am able to send the second message.

The first message is something like;

*HEAD /TIPS/LAWLER/PANOHOW2.PDF HTTP/1.0\r\n   
HTTP/1.0\r\n  
Connection: close\r\n  
\r\n*

and response is;

*HTTP/1.1 200 OK  
Date: Mon, 24 Jan 2011 10:53:38 GMT  
Server: Apache  
Last-Modified: Tue,  
22 Sep 1998 13:19:52 GMT  
ETag: "1968013-2b4f4-3386e15b6ee00"  
Accept-Ranges: bytes  
Content-Length: 177396  
Connection: close  
Content-Type: application/pdf*

When i attemp to sen message;

GET /TIPS/LAWLER/hedeh/PANOHOW2.PDF HTTP/1.0\r\n  
Range: bytes=0-44349\r\n  
Connection: close\r\n   
\r\n

I get nothing.

What is wrong with my code?

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {



            //Parse URL
            String cmd = "http://www.imaging-resource.com"; //Host Name
            if (cmd.contains("http://"))
            {
                cmd = cmd.substring(7); //
                if (cmd.contains("/"))
                {
                    int index = cmd.indexOf("/");
                    cmd = cmd.substring(0, index);
                    System.out.println(cmd);
                }
            }
            String str = "HEAD /TIPS/LAWLER/PANOHOW2.PDF HTTP/1.0\r\nConnection: close\r\n\r\n"; //First message to send




            //Create socket, connect, initialize read and write handlers
            //in, out
            Socket socket = null;           //Create a client socket
            SocketAddress sockaddr = null;
            InetAddress address = null;
            InputStream input = null;      //Input handler
            OutputStream output = null;    //Output handler

            try
            {
                address = InetAddress.getByName(cmd);           //Get ip using host name
                socket = new Socket();                          //Contrusct Socket
                sockaddr = new InetSocketAddress(address, 80);
                //socket.setTcpNoDelay(false);
                socket.connect(sockaddr, 2000);                 //Connect to server set and timeout to 2 sec
            } //End of try Block
            catch (Exception ex)
            {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println(ex);
            } //End of catch Block

             if (!socket.isConnected())
             {
                System.out.println("not connected");
                System.exit(-1);
             }


            //Sending package here
            try
            {
                int c;
                byte[] buf = new byte[65535];
                char[] chr = new char[65535];


                input = socket.getInputStream();            //Input handler is created
                output = socket.getOutputStream();          //Output handler is created
                buf = str.getBytes();                       //HEAD message converted into byte array
                output.write(buf);                          //Sending message to server
                output.flush();
                int counter = 0;


                while ((c = input.read()) != -1)        //Reading received package
                    chr[counter++]=(char)c;


                //input.reset();
                str = new String(chr);                  //For better manipulation, server message is converted to string
                System.out.println(str);

            } catch (IOException e)
            {
                System.err.print(e);
            } //End of catch








            int index = str.indexOf("Content-Length");  //Look for "Content-Length" in response
            str = str.substring(index);                 //Using its beginning index create an substring           
            index = str.indexOf("\r\n");                //Search for end of line
            str = str.substring(0, index);              //Erase end if line chars   - \r\n
            str = str.substring(16, str.length());      //"Content-Length: " 16 chars
            int fileSize = Integer.parseInt(str);       //Lentgh of file is converted to Integer


            int[][] parts = new int[4][2];              //Beginning and en of jobs for threads will be stored here
            int remainder = fileSize;                   //Bytes left to split for rest of the threads will be stored here
            int start = 0;
            int finish = 0;

            for (int i = 0; i < 4; i++)                 //Number of threads many times
            {
                parts[i][0] = start;                        //*******Each threads job Interval(eg. 0-108)
                //System.out.print(parts[i][0] + "-");      //******
                finish += remainder / 4 - i;                //*****
                parts[i][1] = finish;                       //****
                start = finish + 1;                         //***

                if (i + 1 == 4)
                parts[i][1] = fileSize;                     //*
            }

            str = "GET /TIPS/LAWLER/hedeh/PANOHOW2.PDF HTTP/1.0\r\nRange: bytes=" + parts[0][0] + "-" + parts[0][1] + "\r\nConnection: close\r\n\r\n";
            //System.out.println(str);


           if(!socket.isConnected())
           {
               System.out.println("closed");
               try
               {
                    socket.connect(sockaddr, 2000);
               }//End od try
               catch(Exception e){
                System.err.print(e);
                }//End of catch
            }//End of If
           System.out.println("Is Outputhandler closed :"+socket.isOutputShutdown());
           System.out.println("Is Inputhandler closed :"+socket.isInputShutdown());



          try
          {

               int c;
               byte[] buf = new byte[65535];
               char[] chr = new char[65535];


                buf = str.getBytes();                      //Output handler is created
                output.write(buf);                         //Sending message to server
                output.flush();
                int counter = 0;

                if((c = input.read()) != -1)
                {
                    chr[counter++] = (char) c;

                    while ((c = input.read()) != -1)                //Reading received package
                    {
                        System.out.println("response is not -1");
                        chr[counter++]=(char)c;
                    }


                    str = new String(chr);                  //For better manipulation, serve message is converted to string
                    System.out.println("Response "+str);
                }//End of If

                else System.out.println("No Response!");


            }catch(Exception e)
            {System.err.print(e);}

            //Closing open stuff
             try {
                output.close();
                input.close();
                socket.close();
            } catch (Exception e) {
                System.out.println(e);
            }




    }// End of main method
}//End of class definition
  • 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-19T11:27:34+00:00Added an answer on May 19, 2026 at 11:27 am

    The first message is something like;

    HTTP/1.0\r\n  
    

    You have to use HTTP version 1.1 to use multiple requests on a single TCP connection.

    From the Wikipedia article on HTTP:

    In HTTP/0.9 and 1.0, the connection is closed after a single request/response pair. In HTTP/1.1 a keep-alive-mechanism was introduced, where a connection could be reused for more than one request.


    Also, as @Joachim Sauer points out in the comments, you’re explicitly saying Connection: close in your header. 🙂

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I am currently running into a problem where an element is coming back from
I am writing an app with both english and french support. The app requests

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.