substring字符串越界.
注意 -1 和 超过了字符串长度 这两种情况.

解决方案 »

  1.   

    import  java.io.*; 
    import  java.net.*;
    import  java.util.*;   
    public class ServerThread extends Thread { 
      
        private Socket socket = null; 
      
        public ServerThread (Socket socket) { 
            super ("ServerThread"); 
            this.socket = socket; 
        } 
      
        public void run ( ) { 
      
                try { 
                BufferedReader in = new BufferedReader (new InputStreamReader (socket.getInputStream ( ))); 
                PrintWriter out = new PrintWriter (socket.getOutputStream ( ),true); 
      
                String ClientInput; 
                while (true) { 
                    ClientInput = in.readLine ( ); 
                    if (ClientInput == null)
                       break; 
                    
                             if(Word_count(ClientInput)!=3){ // check the invlidatoin of the request
                        
                                out.println("error request");
                           
                                  break;}
                     
                                      if((ClientInput.startsWith("GET")) || (ClientInput.startsWith("HEAD")) ||
                                          (ClientInput.startsWith("POST"))) {
                                
                                         int pathStart = ClientInput.indexOf("/");
     
                                         int pathEnd   = ClientInput.indexOf(' ',pathStart);
     
                                         int typeEnd   = ClientInput.indexOf('0',pathEnd);
                                
                                         String pathContent = ClientInput.substring(pathStart+1,pathEnd);
                                          
                                         String pathAddress = ClientInput.substring(pathStart,pathEnd);
                                           
                                         String type_Of_Http = ClientInput.substring(pathEnd+1,typeEnd+1);                                                 
     if( (pathAddress.equals("/"))&&(type_Of_Http.equals("HTTP/1.0")) ){
        out.println("HTTP 1.1 200 OK\r\n" + "Connection:  close\r\n" + "Content-Type: text/html\r\n" + 
                    "\r\n" + 
                    "<HTML>\n" + 
                    "<!DOCTYPE HTML PUBLIC " + 
                      "\"-//W3C//DTD HTML 4.0 Transitional//EN\">\n" +
                    "<HEAD>\n" +
                    "<TITLE> DEFAULT Results</TITLE>\n" +
                    "</HEAD>\n" +
                    "\n" + 
                    "<BODY BGCOLOR=\"#FDF5E6\">\n" +
                    "<H1 ALIGN=\"CENTER\">" + "DEFAULT" +" Results</H1>\n" +
                    "Here is the request line and request headers\n" +
                    "sent by your browser:\n" +
                    "<PRE>" +
                    "</PRE>\n" +
                    "</BODY>\n" +
                    "</HTML>\n");   
                    break;                      
                    }
             /**
                                   FileInputStream f = new FileInputStream (pathAddress);
                              
                                   DataInputStream html = new DataInputStream (f); 
                                 
                                 
                                  */      
                         }            
                    
                          //if(ClientInput
                out.println (Word_count(ClientInput)); 
                } 
      
                System.out.println ("Client exit."); 
      
                out.close ( ); 
                in.close ( ); 
                socket.close ( ); 
            } 
            catch (IOException e) { 
                e.printStackTrace ( ); 
                System.exit (1); 
            } 
        }     public static int Word_count (String a) {        int count =0;
            StringTokenizer st = new StringTokenizer(a);
              while (st.hasMoreTokens()){
      st.nextToken();
                      count++;}
          return count;
        }
        
         
    }
      

  2.   

    int pathStart = ClientInput.indexOf("/");
     
    int pathEnd   = ClientInput.indexOf(' ',pathStart);
     
    int typeEnd   = ClientInput.indexOf('0',pathEnd);
                                
    String pathContent = ClientInput.substring(pathStart+1,pathEnd);
                                          
    String pathAddress = ClientInput.substring(pathStart,pathEnd);
                                           
    String type_Of_Http = ClientInput.substring(pathEnd+1,typeEnd+1);
    是你的substring出错。我不知道怎么测试它。所以你在用substring函数前,最好检查一下typeEnd、pathEnd、pathStart、以及typeEnd+1、pathEnd + 1、pathStart + 1的有效性。
      

  3.   

    水管漏水你知道把破的地方堵上,这个也一样:比如:
    String pathAddress = ClientInput.substring(pathStart,pathEnd);
    // 修改一下:
    String pathAddress = "";
    if (pathStart>-1 && pathEnd<=ClientInput)
            pathAddress = ClientInput.substring(pathStart,pathEnd);