2012-11-23 4 views
1

Java 소켓에 다른 문제가 있습니다. 서버의 소켓은 요청을 해석하지 않습니다. HTML 헤더 뒤에 줄을 읽는 데 문제가 있다고 생각하지만이 코드가 무엇이 잘못되었는지 전혀 알지 못합니다. TIA. 내가 할읽기 요청이 서버를 중지합니다

@Override 
public void run() { 
    DataOutputStream dout = null; 
    BufferedReader reader = null; 
    try { 
     dout = new DataOutputStream(socket.getOutputStream()); 
     reader = new BufferedReader(
       new InputStreamReader(
         socket.getInputStream(), UTF-8")); 

     String requestString = reader.readLine(); 
     StringTokenizer tokenizer = new StringTokenizer(requestString); 
     String httpMethod = tokenizer.nextToken(); 
     String httpQueryString = tokenizer.nextToken(); 

     System.out.println("method: " + httpMethod); 
     System.out.println("query: " + httpQueryString); 
     String line; 
     int i = 0; 
     while (! (line = reader.readLine()) 
       .equals("")) { 
      System.out.println(i++ + " : " + line); 
     } 

     //DEBUG 
     System.out.println("foo"); 
    // HERE IS THE PROBLEM !!! 
     line = reader.readLine(); 
     System.out.println("aaa " + line); 
     line = reader.readLine(); 
     System.out.println("bbb " + line); 
     line = reader.readLine(); 
     System.out.println("ccc " + line); 

     // Pseudocode 
     if (GET) { 
      if ("/") { 
       ... 
      } else if (isFile) { 
       ... 
      } else { 
       ... 
      } 
     } else if (POST) { 
      ... //TODO 
     } else { 
      Error 404 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     //Cleaning 
     try { 
      reader.close(); 
      dout.close(); 
      socket.close(); 
     } catch (IOException e) { 
      Logger.getAnonymousLogger().warning("Socket cannot be closed"); 
     } 
    } 
} 

그리고 출력 :

INFO: Server is RUNNING 
INFO: Connection accepted 
method: GET 
query:/
0 : Host: 127.0.0.1:8001 
1 : User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0 
2 : Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
3 : Accept-Language: pl,en-us;q=0.7,en;q=0.3 
4 : Accept-Encoding: gzip, deflate 
5 : DNT: 1 
6 : Connection: keep-alive 
7 : Cache-Control: max-age=0 
foo 

--- in this place server halts --- 
--- then I refresh page or do anything else that sends request (GET, POST) --- 
--- and server receives 'remaining' part of the request --- 

aaa null // in POST this line has send values 
bbb null 
ccc null 
INFO: method = GET 
INFO: Connection accepted 
method: GET 
query:/
0 : Host: 127.0.0.1:8001 
1 : User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0 
2 : Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
3 : Accept-Language: pl,en-us;q=0.7,en;q=0.3 
4 : Accept-Encoding: gzip, deflate 
5 : DNT: 1 
6 : Connection: keep-alive 
foo 

--- in this place server halts --- 
--- then I refresh page or do anything else that sends request (GET, POST) --- 
--- and server receives 'remaining' part of the request --- 

답변

0

요청의 어떠한 나머지 부분이 없습니다

여기에 코드 조각입니다. 빈 줄 다음에 GET 요청이 중지됩니다. 들어오는 데이터가 없어 서버가 차단되었습니다 (다음 요청까지)

+0

예, 문제를 해결하려면 어떻게해야합니까? 해야 할 일 : 1. 고객으로부터 요청을받습니다. 2.이 요청에 응답하십시오. 3. 나머지 부분을 받으십시오. 4. 그걸로 뭔가를하십시오. ? – Miki

+0

직접 HTTP 서버를 구현하지 마십시오. 서블릿이나 무언가를 사용하십시오. – irreputable

+0

나는해야한다. 그것은 프로그래밍 수업의 프로젝트입니다. 헤더 (또는 POST 메시지 본문) 나머지를 가져 오려면 "200 OK"응답을 보내야한다는 것을 알았습니다. 그렇다면 검색 결과가있는 페이지를 다시 보내려면 어떻게해야합니까? – Miki

관련 문제