2017-12-09 21 views
0

HTTP 서버를 만들 때 약간의 시간을 할애하여 TcpListener 클래스를 사용하기로 결정했습니다 (이전에 HttpListener을 사용함). 문제는 각 브라우저에서 나에게 "Connection refused"라는 메시지를 제공한다는 것입니다. 브라우저가 일반적으로 내용 (이 경우 html 페이지)과 200 코드로 http 헤더를 가져 오기 때문에 매우 이상합니다. 더구나 나는 내 페이지를 대략 0.5 초 동안 본 다음 사라진다.WebBrowser : 연결이 거부되었습니다

 WebServer ws = new WebServer(SendResponse, address); 
     Thread thread = new Thread(new ThreadStart(ws.Run)); 
     thread.Start(); 

웹 서버 클래스 :

public void Run() { 
     _listener = new TcpListener(IPAddress.Any, 8080); 
     _listener.Start(); 

     while(isRunning) 
     { 
      TcpClient client = _listener.AcceptTcpClient(); 
      Thread thread = new Thread(() => Connection(client)); 
      thread.Start(); 
      Thread.Sleep(1); 
     } 
    } 
    public void Connection(TcpClient client) 
    { 
     NetworkStream stream = client.GetStream(); 

     string response = "HTTP/1.0 200 OK\r\n" 
      + "Content-Type: text/html\r\n" 
      + "Connection: close\r\n" 
      + "\r\n"; 

     byte[] bytesResponse = Encoding.ASCII.GetBytes(response); 
     byte[] data = Encoding.ASCII.GetBytes("<!doctype html><html><body><h1>test server</h1></body></html>"); 

     stream.Write(bytesResponse, 0, bytesResponse.Length); 
     stream.Write(data, 0, data.Length); 
     stream.Dispose(); 
    } 

나는 그것이 방화벽 잘못이 아니에요 생각 때문에 다른 C#을 서버, 예를 들어이 하나 마법처럼 https://www.codeproject.com/Articles/137979/Simple-HTTP-Server-in-C 작동합니다. 뭐가 문제 야?

답변

0

내 문제에 대한 해결책을 찾았습니다. 헤더 Content-Length가 누락되었습니다.