Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 6.80 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Java stream socket can only send once
  2. *HEAD /TIPS/LAWLER/PANOHOW2.PDF HTTP/1.0rn  
  3. HTTP/1.0rn  
  4. Connection: closern  
  5. rn*
  6.        
  7. *HTTP/1.1 200 OK  
  8. Date: Mon, 24 Jan 2011 10:53:38 GMT  
  9. Server: Apache  
  10. Last-Modified: Tue,  
  11. 22 Sep 1998 13:19:52 GMT  
  12. ETag: "1968013-2b4f4-3386e15b6ee00"  
  13. Accept-Ranges: bytes  
  14. Content-Length: 177396  
  15. Connection: close  
  16. Content-Type: application/pdf*
  17.        
  18. GET /TIPS/LAWLER/hedeh/PANOHOW2.PDF HTTP/1.0rn  
  19. Range: bytes=0-44349rn  
  20. Connection: closern  
  21. rn
  22.        
  23. public class Main {
  24.  
  25.     /**
  26.      * @param args the command line arguments
  27.      */
  28.     public static void main(String[] args) {
  29.  
  30.  
  31.  
  32.             //Parse URL
  33.             String cmd = "http://www.imaging-resource.com"; //Host Name
  34.             if (cmd.contains("http://"))
  35.             {
  36.                 cmd = cmd.substring(7); //
  37.                 if (cmd.contains("/"))
  38.                 {
  39.                     int index = cmd.indexOf("/");
  40.                     cmd = cmd.substring(0, index);
  41.                     System.out.println(cmd);
  42.                 }
  43.             }
  44.             String str = "HEAD /TIPS/LAWLER/PANOHOW2.PDF HTTP/1.0rnConnection: closernrn"; //First message to send
  45.  
  46.  
  47.  
  48.  
  49.             //Create socket, connect, initialize read and write handlers
  50.             //in, out
  51.             Socket socket = null;           //Create a client socket
  52.             SocketAddress sockaddr = null;
  53.             InetAddress address = null;
  54.             InputStream input = null;      //Input handler
  55.             OutputStream output = null;    //Output handler
  56.  
  57.             try
  58.             {
  59.                 address = InetAddress.getByName(cmd);           //Get ip using host name
  60.                 socket = new Socket();                          //Contrusct Socket
  61.                 sockaddr = new InetSocketAddress(address, 80);
  62.                 //socket.setTcpNoDelay(false);
  63.                 socket.connect(sockaddr, 2000);                 //Connect to server set and timeout to 2 sec
  64.             } //End of try Block
  65.             catch (Exception ex)
  66.             {
  67.                 Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  68.                 System.out.println(ex);
  69.             } //End of catch Block
  70.  
  71.              if (!socket.isConnected())
  72.              {
  73.                 System.out.println("not connected");
  74.                 System.exit(-1);
  75.              }
  76.  
  77.  
  78.             //Sending package here
  79.             try
  80.             {
  81.                 int c;
  82.                 byte[] buf = new byte[65535];
  83.                 char[] chr = new char[65535];
  84.  
  85.  
  86.                 input = socket.getInputStream();            //Input handler is created
  87.                 output = socket.getOutputStream();          //Output handler is created
  88.                 buf = str.getBytes();                       //HEAD message converted into byte array
  89.                 output.write(buf);                          //Sending message to server
  90.                 output.flush();
  91.                 int counter = 0;
  92.  
  93.  
  94.                 while ((c = input.read()) != -1)        //Reading received package
  95.                     chr[counter++]=(char)c;
  96.  
  97.  
  98.                 //input.reset();
  99.                 str = new String(chr);                  //For better manipulation, server message is converted to string
  100.                 System.out.println(str);
  101.  
  102.             } catch (IOException e)
  103.             {
  104.                 System.err.print(e);
  105.             } //End of catch
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.             int index = str.indexOf("Content-Length");  //Look for "Content-Length" in response
  115.             str = str.substring(index);                 //Using its beginning index create an substring          
  116.             index = str.indexOf("rn");                //Search for end of line
  117.             str = str.substring(0, index);              //Erase end if line chars   - rn
  118.             str = str.substring(16, str.length());      //"Content-Length: " 16 chars
  119.             int fileSize = Integer.parseInt(str);       //Lentgh of file is converted to Integer
  120.  
  121.  
  122.             int[][] parts = new int[4][2];              //Beginning and en of jobs for threads will be stored here
  123.             int remainder = fileSize;                   //Bytes left to split for rest of the threads will be stored here
  124.             int start = 0;
  125.             int finish = 0;
  126.  
  127.             for (int i = 0; i < 4; i++)                 //Number of threads many times
  128.             {
  129.                 parts[i][0] = start;                        //*******Each threads job Interval(eg. 0-108)
  130.                 //System.out.print(parts[i][0] + "-");      //******
  131.                 finish += remainder / 4 - i;                //*****
  132.                 parts[i][1] = finish;                       //****
  133.                 start = finish + 1;                         //***
  134.  
  135.                 if (i + 1 == 4)
  136.                 parts[i][1] = fileSize;                     //*
  137.             }
  138.  
  139.             str = "GET /TIPS/LAWLER/hedeh/PANOHOW2.PDF HTTP/1.0rnRange: bytes=" + parts[0][0] + "-" + parts[0][1] + "rnConnection: closernrn";
  140.             //System.out.println(str);
  141.  
  142.  
  143.            if(!socket.isConnected())
  144.            {
  145.                System.out.println("closed");
  146.                try
  147.                {
  148.                     socket.connect(sockaddr, 2000);
  149.                }//End od try
  150.                catch(Exception e){
  151.                 System.err.print(e);
  152.                 }//End of catch
  153.             }//End of If
  154.            System.out.println("Is Outputhandler closed :"+socket.isOutputShutdown());
  155.            System.out.println("Is Inputhandler closed :"+socket.isInputShutdown());
  156.  
  157.  
  158.  
  159.           try
  160.           {
  161.  
  162.                int c;
  163.                byte[] buf = new byte[65535];
  164.                char[] chr = new char[65535];
  165.  
  166.  
  167.                 buf = str.getBytes();                      //Output handler is created
  168.                 output.write(buf);                         //Sending message to server
  169.                 output.flush();
  170.                 int counter = 0;
  171.  
  172.                 if((c = input.read()) != -1)
  173.                 {
  174.                     chr[counter++] = (char) c;
  175.  
  176.                     while ((c = input.read()) != -1)                //Reading received package
  177.                     {
  178.                         System.out.println("response is not -1");
  179.                         chr[counter++]=(char)c;
  180.                     }
  181.  
  182.  
  183.                     str = new String(chr);                  //For better manipulation, serve message is converted to string
  184.                     System.out.println("Response "+str);
  185.                 }//End of If
  186.  
  187.                 else System.out.println("No Response!");
  188.  
  189.  
  190.             }catch(Exception e)
  191.             {System.err.print(e);}
  192.  
  193.             //Closing open stuff
  194.              try {
  195.                 output.close();
  196.                 input.close();
  197.                 socket.close();
  198.             } catch (Exception e) {
  199.                 System.out.println(e);
  200.             }
  201.  
  202.  
  203.  
  204.  
  205.     }// End of main method
  206. }//End of class definition
  207.        
  208. HTTP/1.0rn