回首頁

線上論壇

線上資源

資源下載

新聞與活動

技術文章

關於Move-To.NET

Whidbey專欄

徵才&接案

登入
加入會員

還沒成為會員嗎?
趕快加入吧~

網站後台
累計:49555小時未停機
伺服器平台: 程式平台: 開發工具: 資料庫:
建置人力: 2 人-週
語言: C#/VB.NET
主機:HP-ProLiant
機型:DL380 G3

C#

作者 主題: C# web srevre 有人能幫我翻譯成中文解釋嗎 >"<
sleepy__c
C# web srevre 有人能幫我翻譯成中文解釋嗎 >"<
發表時間: Sunday, March 29, 2009 5:49 PM (台)

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.IO;
using System.Web;

public class WebServer
{
   public static void Main()
   {
      IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 8080);

      Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

      newsock.Bind(ipep);
      newsock.Listen(10);
  
      while(true)
      {
   Socket client = newsock.Accept();
   IPEndPoint clientep = (IPEndPoint) client.RemoteEndPoint;
        
         // create a new thread and then receive message.
         HttpListener listener = new HttpListener(client);
         Thread thread = new Thread(new ThreadStart(listener.run));
         thread.Start();
      }
   }
}

class HttpListener
{
    String[] map ={"mpeg=video/mpeg", "mpg=video/mpeg", "wav=audio/x-wav", "jpg=image/jpeg", "gif=image/gif", "zip=application/zip", "pdf=application/pdf", "xls=application/vnd.ms-excel", "ppt=application/vnd.ms-powerpoint", "doc=application/msword", "htm=text/html", "html=text/html", "css=text/plain", "vbs=text/plain", "js=text/plain", "txt=text/plain", "java=text/plain"};
    Socket socket;
    NetworkStream stream;
    String header;
    String root = ".";

    public HttpListener(Socket s)
    {
        socket = s;
    }

    public void run()
    {
     stream = new NetworkStream(socket);
        request();
        response();
        stream.Close();
    }

    public void send(String str) {
        socket.Send(Encoding.UTF8.GetBytes(str));
    }

    public static String innerText(String pText, String beginMark, String endMark)
    {
        int beginStart = pText.IndexOf(beginMark);
        if (beginStart < 0) return null;
        int beginEnd = beginStart + beginMark.Length;
        int endStart = pText.IndexOf(endMark, beginEnd);
        if (endStart < 0) return null;
        return pText.Substring(beginEnd, endStart - beginEnd);
    }

    public void request()
    {
        StreamReader reader = new StreamReader(stream);
        header = "";
        while (true)
        {
            String line = reader.ReadLine();
            Console.WriteLine(line);
            if (line.Trim().Length == 0)
                break;
            header += line + "\n";
        }
    }
   
    void response()
    {
      try
      {
        Console.WriteLine("========response()==========");
        String path = innerText(header, "GET ", "HTTP/").Trim(); // 取得檔案路徑 : GET 版。
        HttpUtility.UrlDecode(path);
        String fullPath = root+path;
        FileInfo info = new FileInfo(fullPath);
        if (!info.Exists)
            throw new Exception("File not found !");
        send("HTTP/1.0 200 OK\n");
        send("Content-Type: "+type(fullPath)+"\n");
        send("Content-Length: "+info.Length+"\n");
        send("\n");
        byte[] buffer = new byte[4096];
        FileStream fileStream = File.OpenRead(fullPath);
        while (true)
        {
            int len = fileStream.Read(buffer, 0, buffer.Length);
            socket.Send(buffer, 0, len, SocketFlags.None);
            if (len < buffer.Length) break;
        }
        fileStream.Close();
      } catch {
        send("HTTP/1.0 404 Error\n");
        send("\n");
      }
    }

    String type(String path)
    {
        String type = "*/*";
        path = path.ToLower();
        for (int i = 0; i < map.Length; i++)
        {
            String[] tokens = map[i].Split('=');
            String ext = tokens[0], mime = tokens[1];
            if (path.EndsWith("." + ext)) type = mime;
        }
        return type;
    }
}


回覆:

Author Thread:
623
C# web srevre 有人能幫我翻譯成中文解釋嗎 >"<
Posted: Tuesday, March 31, 2009 7:51 PM (台)
問 ccc 最快拉 ...

歡迎新會員
073415793
會員人數:
7122
Microsoft MSDN 網站
弈飛資訊 網站

 

有智慧才能分辨善惡邪正;有謙虛才能建立美滿人生。

 

最新發表 

  • C# web srevre 有人能幫我翻譯成中文解釋嗎 >"<
    Posted by sleepy__c on Sunday, March 29, 2009 (台)

  • 徵才-J2EE軟體開發工程師
    Posted by manpower601 on Friday, February 13, 2009 (台)

  • 以色列外商在台成立研發中心 - 網羅.NET高手
    Posted by Juwita on Thursday, January 08, 2009 (台)

  •  

    最新回應 

  • 如何在Multi-thread的情形下,控制Form元件,例如ListBox???
    Posted by wywcinema on Monday, September 28, 2009 (台)

  • 如何在Multi-thread的情形下,控制Form元件,例如ListBox???
    Posted by wywcinema on Monday, September 28, 2009 (台)

  • 如何在Multi-thread的情形下,控制Form元件,例如ListBox???
    Posted by wywcinema on Monday, September 28, 2009 (台)

  •  

    最新連結 

  • 下載微軟最新 Visual Studio .NET 技術名詞小幫手
    Microsoft Terminology Assistant 是一個視窗架構的小幫手工具,它可以立即協助您找出滑鼠所指的技術名詞原文和定義。
    當您在閱讀電腦上的技術說明文件時,您可以使用 TA 這個工具根據您滑鼠游標的位置立即協助您找出滑鼠所指的技術名詞原文和定義;這工具也提供軟體技術辭彙字典的功能,讓您能直接查出並不瞭解的技術名詞。您一定可以使用這個小程式來使您能更快、更準確的學習最新的軟體技術,加速您學習軟體技術的時間。


  • DBA 的 SQL Server Yukon 概觀
    這篇文章提供有關資料庫系統管理的新功能,以及資料庫可用性、延展性與安全性的概觀。

  • 伺服器端 ASP .NET 資料繫結之二:自訂 DataGrid 控制項
    DataGrid 是網頁編輯最常使用的控制,但是DataGrid 雖然好用卻有一些使用上的限制,本篇文章介紹了如何自訂DataGrid來滿足所須的功能。



  •  
    本網站由弈飛資訊建置維護