2017-12-09 9 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(); 

WebServerのクラス:

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#のサーバ、例えば、この1:魔法のようにhttps://www.codeproject.com/Articles/137979/Simple-HTTP-Server-in-C作品。どうしましたか?

答えて

0

私の問題に対する答えが見つかりました。 ヘッダーContent-Lengthが見つかりませんでした:

関連する問題