2013-10-08 4 views
4

안녕하세요 저는 Java에서 다중 스레드 HTTP 서버를 만들었으며 웹 브라우저를 사용할 때 컨텐츠를 가져올 수 없습니다. 텔넷 클라이언트를 사용하여 웹 서버에서 읽을 때 제대로 작동하지만 크롬과 같은 브라우저에서는 아무 것도 표시되지 않습니다. 나는 텔넷을 통한 연결이 잘 작동하는지 보여주는 및 사용 크롬 browswer을 통해 웹 서버를 호출 할 때 Wireshark를 캡처를 보여줍니다 추가 사진 아래 3 개 장의 사진을 게시 한 :HTTP 서버가 브라우저와 작동하지 않습니다.

http://localhost:4568 

Telnet connection to web server 200

Telnet connection to Web server 404

wireshark

아래에 작성한 코드는 다음과 같습니다. 디버깅을 쉽게하기 위해 코드를 싱글 스레드로 변경했습니다. "디버깅 섹션"이라고하는 MainWebServer의 섹션은 스레딩 클래스에 포함되어 있습니다.

MainWebServer :

package my.simple.webserver; 

import java.io.*; 
import java.net.*; 
import java.util.*; 

@SuppressWarnings("unused") 
public class mainWebServer { 

private static ServerSocket ser = null; 
private static Socket cli = null; 
private static String host = null; 
private static int port; 
/** 
* @param args 
*/ 
public static void main(String[] args) { 
    // Get parameters 
    if(args.length < 2) 
    { 
     setHost("localhost"); 
     port = 4568;   
    } 
    else 
    { 
     setHost(args[0]); 
     port = Integer.parseInt(args[1]); 
    } 

    // initialize server 
    try { 
     // change to take in host 
     ser = new ServerSocket(port); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    while(true) 
    { 
     try { 
      setCli(ser.accept()); 
      //(new Thread(new HttpThread(cli))).start(); 

      // Debugging section 
      DataInputStream sockIn = new  DataInputStream(cli.getInputStream()); 
      HttpRequest req = new HttpRequest(cli); 
      int cnt = 0; 
      byte[] buffer = new byte[1024]; 
      while((cnt = sockIn.read(buffer)) >= 0) 
      { 
       System.out.println("We are here"); 
       req.checkMethod(new String(buffer)); 
       resetBuffer(buffer, 256); 
      } 
      // end debugging section 

      // run thread for client socket 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    } 
    public static String getHost() { 
    return host; 
    } 
    public static void setHost(String host) { 
    mainWebServer.host = host; 
    } 
    public static Socket getCli() { 
    return cli; 
    } 
    public static void setCli(Socket cli) { 
    mainWebServer.cli = cli; 
    } 

    // remove after debugging 
    public static void resetBuffer(byte[] buffer2, int i) { 
     // TODO Auto-generated method stub 
     for(int x=0; x < i; x++) 
     { 
     buffer2[x] = 0; 
     } 
    } 
} 

HttpRequest를 클래스 :

package my.simple.webserver; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.net.Socket; 
import java.util.NoSuchElementException; 
import java.util.StringTokenizer; 

public class HttpRequest { 
    private String method = null; 
    private String path = null; 
    private FileInputStream fis = null; 
    private OutputStream os = null; 
    private Socket mycli; 

    private static final String HTTP_OK_RESPONSE = "HTTP/1.1 200 OK\r\n"; 
    private static final String HTTP_FNF_RESPONSE = "HTTP/1.1 404 NOT FOUND\r\n"; 
    private static final String HTTP_DATE_RESPONSE = "Date: Mon, 04-Jan-99 13:14:15 GMT\r\n"; 
    private static final String HTTP_SERVER_RESPONSE = "Server: Challenger\r\n"; 
    private static final String HTTP_CONTENT_TYPE = "Content-Type: text/html\r\n"; 
    private String HTTP_Content_Length = "Content-length: "; 

    public HttpRequest(Socket myCli) { 
     // TODO Auto-generated constructor stub 
     mycli = myCli; 
    } 

    void checkMethod(String buffer) 
    { 
     // get data and parse for method, location and protocol 
     // use switch statement based on method given 
     System.out.println("buffer = " + buffer); 
     StringTokenizer tok = new StringTokenizer(buffer); 

     try 
     { 
      method = tok.nextToken(); 
      path = tok.nextToken(); 
     } 
     catch(NoSuchElementException nse) 
     { 
      System.out.println(nse.getMessage()); 
      method = ""; 
     } 
     //System.out.println("method=" + method + " path=" + path); 

     switch(method) 
     { 
      case "GET": 
       //System.out.println("Get method called"); 
       getMethod(); 
       break; 
      case "HEAD": 
       System.out.println("Head method called"); 
       break; 
      case "POST": 
       System.out.println("Post method called"); 
       break; 
      case "PUT": 
       System.out.println("Put method called"); 
       break; 
      case "DELETE": 
       System.out.println("Delete method called"); 
       break; 
      case "TRACE": 
       System.out.println("Trace method called"); 
       break; 
      case "OPTIONS": 
       System.out.println("Options method called"); 
       break; 
      case "CONNECT": 
       System.out.println("Connect method called"); 
       break; 
      default: 
       break; 
     } 
    } 

    private void getMethod() 
    { 
     String file; 
     File f = null; 
     // check if file exists 
     if(path.equalsIgnoreCase("/")) 
     { 
      //file = checkForIndex(); 
      file = "index.html"; 
     } 
     else 
      file = path; 
     System.out.println("file = " + file); 
     // open file 
     try { 
      file = "badfile.html"; 
      f = new File(file); 
      os = mycli.getOutputStream(); 
      //check if file exists 
      if(f.exists()) 
      { 
       byte[] buffer = null; 
       buffer = HTTP_OK_RESPONSE.getBytes(); 
       os.write(buffer); 
       buffer = HTTP_DATE_RESPONSE.getBytes(); 
       os.write(buffer); 
       buffer = HTTP_SERVER_RESPONSE.getBytes(); 
       os.write(buffer); 
       buffer = HTTP_CONTENT_TYPE.getBytes(); 
       os.write(buffer); 

       long fileLen = f.length(); 
       HTTP_Content_Length = HTTP_Content_Length.concat(String.valueOf(fileLen)); 
       HTTP_Content_Length = HTTP_Content_Length.concat("\r\n"); 
       buffer = HTTP_Content_Length.getBytes(); 
       os.write(buffer); 

       fis = new FileInputStream(file); 

       int nread, result; 
       // read file 
       while((nread = fis.available()) > 0) 
       { 
        buffer = new byte[nread]; 
        result = fis.read(buffer); 
        if(result == -1) break; 
        os.write(buffer); 
       } 
       System.out.println("Left the loop!"); 
       mycli.close(); 
      } 
      else 
      { 
       // deal with 404 
       byte[] buffer = null; 
       buffer = HTTP_FNF_RESPONSE.getBytes(); 
       os.write(buffer); 
       buffer = HTTP_DATE_RESPONSE.getBytes(); 
       os.write(buffer); 
       buffer = HTTP_SERVER_RESPONSE.getBytes(); 
       os.write(buffer); 
       buffer = HTTP_CONTENT_TYPE.getBytes(); 
       os.write(buffer); 

       f = new File("fnf.html"); 
       long fileLen = f.length(); 
       HTTP_Content_Length = HTTP_Content_Length.concat(String.valueOf(fileLen)); 
       HTTP_Content_Length = HTTP_Content_Length.concat("\r\n"); 
       buffer = HTTP_Content_Length.getBytes(); 
       os.write(buffer); 

       fis = new FileInputStream(f); 

       int nread, result; 
       // read file 
       while((nread = fis.available()) > 0) 
       { 
        buffer = new byte[nread]; 
        result = fis.read(buffer); 
        if(result == -1) break; 
        os.write(buffer); 
       } 
       System.out.println("Left the loop!"); 
       mycli.close(); 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     // end thread 
    } 
} 

내가 한 번 위기에 오전, 내가 어떤 도움을 주셔서 감사합니다.

감사합니다.

답변

2

HTTP 프로토콜의 헤더를 보낸 후, 당신은 빈 줄을 보낼 필요가 있기 때문에 (\ 연구 \ 없음) 예

HTTP/1.1 200 OK 
Content-Type: text/html    
Content-Length: 21 
\r\n(EMPTY LINE) <- you forgot this one 
<b>Hello World :D</b> 
0

추가에 대한 요청 및 응답 모두 헤더 섹션의 끝을 나타냅니다 " \ r \ n "헤더를 완성한 후. Like :

HTTP_Content_Length = HTTP_Content_Length.concat("\r\n\r\n"); 
관련 문제