Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java流套接字只能发送一次_Java_Sockets_Stream_Inputstream_Outputstream - Fatal编程技术网

Java流套接字只能发送一次

Java流套接字只能发送一次,java,sockets,stream,inputstream,outputstream,Java,Sockets,Stream,Inputstream,Outputstream,我正在编写一个简单的下载加速器。问题是我可以发送和接收一次消息。下次我尝试发送和接收消息时,S服务器没有响应。我甚至不确定我是否能够发送第二条消息 第一条信息是这样的 *HEAD /TIPS/LAWLER/PANOHOW2.PDF HTTP/1.0\r\n HTTP/1.0\r\n Connection: close\r\n \r\n* HTTP/1.0\r\n 反应是 *HTTP/1.1 200 OK Date: Mon, 24 Jan 2011 10:53:38 G

我正在编写一个简单的下载加速器。问题是我可以发送和接收一次消息。下次我尝试发送和接收消息时,S服务器没有响应。我甚至不确定我是否能够发送第二条消息


第一条信息是这样的

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

*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*
当我尝试发送sen消息时

GET /TIPS/LAWLER/hedeh/PANOHOW2.PDF HTTP/1.0\r\n  
Range: bytes=0-44349\r\n  
Connection: close\r\n   
\r\n
我什么也得不到

我的代码有什么问题

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

第一条信息是这样的

*HEAD /TIPS/LAWLER/PANOHOW2.PDF HTTP/1.0\r\n   
HTTP/1.0\r\n  
Connection: close\r\n  
\r\n*
HTTP/1.0\r\n  
必须使用HTTP版本1.1才能在单个TCP连接上使用多个请求

从:

在HTTP/0.9和1.0中,连接在单个请求/响应对之后关闭。在HTTP/1.1中引入了一种保持活动的机制,在这种机制中,一个连接可以用于多个请求


此外,正如@Joachim Sauer在评论中指出的,您明确地说连接:在您的标题中关闭:-

第一条信息类似于:

*HEAD /TIPS/LAWLER/PANOHOW2.PDF HTTP/1.0\r\n   
HTTP/1.0\r\n  
Connection: close\r\n  
\r\n*
HTTP/1.0\r\n  
必须使用HTTP版本1.1才能在单个TCP连接上使用多个请求

从:

在HTTP/0.9和1.0中,连接在单个请求/响应对之后关闭。在HTTP/1.1中引入了一种保持活动的机制,在这种机制中,一个连接可以用于多个请求


此外,正如@Joachim Sauer在评论中指出的,您明确地说连接:在您的标题中关闭:-

我认为问题在于您试图使用普通TCP套接字连接到HTTP服务器。是的,HTTP是TCP之上的,但它是一个复杂的协议,需要知道很多事情。我建议您使用更高级别的API来实现HTTP协议,并为您提供更方便的API


最简单的例子是来自JDK的URL+URLConnection。雅加达的HttpClient可能更好

我认为问题在于您试图使用普通TCP套接字连接到HTTP服务器。是的,HTTP是TCP之上的,但它是一个复杂的协议,需要知道很多事情。我建议您使用更高级别的API来实现HTTP协议,并为您提供更方便的API


最简单的例子是来自JDK的URL+URLConnection。雅加达的HttpClient可能更好

他还发送了一个显式连接:close,因此即使在使用HTTP/1.1时也无法工作。他还发送了一个显式连接:close,因此即使在使用HTTP/1.1时也无法工作。半+1:虽然HTTP的有限子集可以很容易地手动实现,几乎没有一个好的理由这样做:现有大量经过良好测试的HTTP客户端,因此不必手动实现。在本作业中,我被要求明确使用套接字。我希望我能被允许使用HttpClient。半+1:虽然HTTP的有限子集可以很容易地手工实现,但几乎没有一个好的理由这样做:有很多现有的、经过良好测试的HTTP客户端,所以不必手动实现。在本作业中,我被要求明确使用套接字。我希望我被允许使用HttpClient。