2011-04-20 149 views
1

我想實現一個簡單的http csharp服務器,當用戶通過服務器IP連接進行連接時,會向瀏覽器發送諸如index.html之類的文檔。我目前對如何通過網絡瀏覽器發送一個簡單的.html文檔有點困惑,因此連接用戶可以看到它。簡單的C#HTTP/TCP服務器

流出是所有信息都發送到瀏覽器的地方。

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Net.Sockets; 
using System.IO; 
using System.Threading; 

namespace TCP_Socket 
{ 
    class ConnectionThread 
    { 
     public ConnectionThread(Socket socketToHandleConnection) 
     { 
      connection = socketToHandleConnection; 
     } 

     Socket  connection  = null; //TCP/IP socket to handle the actual connection 
     NetworkStream connectionStream = null; 
     BinaryReader inStream   = null; 
     BinaryWriter outStream  = null; 
     String  userName   = null; 

     public void run() 
     { 
      connectionStream = new NetworkStream(connection); 

      inStream = new BinaryReader(connectionStream); 
      outStream = new BinaryWriter(connectionStream); 

      userName = Environment.UserName; 

      byte b = 0; 
      String s = "";  //This will contain all the stuff the browser transmitted, 
           //but "all in one go". 
      try 
      { 
       while (connectionStream.DataAvailable) 
       { 
        b = (byte)inStream.ReadSByte(); 
        Console.Out.Write((char)b); 
        s += (char)b; 
       } 

       String[] items = s.Split();//This will contain all the stuff the browser transmitted, 


      } 
      catch (EndOfStreamException eos) 
      { 
       Console.Out.WriteLine("Unexpected end of stream."); 
       Console.Out.WriteLine("Error caused by " + eos.Message); 
      } 
      Console.Out.WriteLine("End of stream."); 

      String stringOut = "HTTP/ 1.1 200 OK\r\n"; 
      outStream.Write(stringOut.ToCharArray()); 

      stringOut = "Content-Type: text/html\r\n"; 
      outStream.Write(stringOut.ToCharArray()); 

      stringOut = "Date: "; 
      outStream.Write(stringOut.ToCharArray()); 
      stringOut = Convert.ToString(DateTime.Now); 
      outStream.Write(stringOut.ToCharArray()); 
      stringOut = "\r\n"; 
      outStream.Write(stringOut.ToCharArray()); 

      stringOut = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\r\n"; 
      outStream.Write(stringOut.ToCharArray()); 

      stringOut = "<html>\r\n"; 
      outStream.Write(stringOut.ToCharArray()); 

      stringOut = "<body>\r\n"; 
      outStream.Write(stringOut.ToCharArray()); 

      stringOut = "Welcome to <strong>" + userName + "'s </strong>primative HTTP server"; 
      outStream.Write(stringOut.ToCharArray()); 

      stringOut = "</body></html>\r\n"; 
      outStream.Write(stringOut.ToCharArray()); 

      inStream.Close(); 
      outStream.Flush(); 
      outStream.Close(); 
      connectionStream.Close(); 
      connection.Close(); 

      Console.Out.WriteLine("Done; Connection closed."); 
     } 
    } 
} 
+2

你什麼錯誤+ – jgauffin 2011-04-20 08:28:47

回答

2

你不需要全部使用套接字作爲HttpListener HTTP代碼已經從.NET 2.0提供的寫。如果它符合您的要求,請嘗試一下。

+0

與HttpListener是你必須運行在OS的東西之前,它會讓你接受連接,即 的netsh的http添加urlacl URL = HTTP缺點:// +:80/MyUri用戶= DOMAIN \ user – Vdex 2016-10-18 19:36:15

+0

任何備用解決方案?我有一些問題與httplistener工作 – Mahdi 2017-02-25 05:01:57