Exemplo n.º 1
0
        static void Main(string[] args)
        {
            try
            {
                // 取得本機的識別名稱
                string hostname = Dns.GetHostName();

                // 取得主機的DNS資訊
                IPAddress serverIP = Dns.Resolve(hostname).AddressList[0];

                // HTTP Server Port = 80
                string Port = "80";

                System.Net.Sockets.TcpListener tcpListener = new TcpListener(serverIP, Int32.Parse(Port));

                // 開始接聽等候用戶端的網路連線請求
                tcpListener.Start();

                Console.WriteLine("HTTP server started at: " + serverIP.ToString() + ":" + Port);

                HTTPSession httpSession = new HTTPSession(tcpListener);

                // 執行緒
                ThreadStart serverThreadStart = new ThreadStart(httpSession.HTTPSessionThread);
                Thread      serverthread      = new Thread(serverThreadStart);

                serverthread.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            try
            {
                // 判斷作業系統是否支援
                if (!System.Net.HttpListener.IsSupported)
                {
                    Console.WriteLine("作業系統不支援HttpListener類別");
                    return;
                }

                // 取得本機的識別名稱
                string hostname = Dns.GetHostName();

                // 取得主機的DNS資訊
                IPAddress serverIP = Dns.Resolve(hostname).AddressList[0];

                // HTTP Server Port = 80
                string Port = "80";

                string prefix = "http://" + serverIP.ToString() + ":" + Port + "/";

                Console.WriteLine("HTTP server started at: " + prefix);

                System.Net.HttpListener httpListener = new System.Net.HttpListener();

                // 設定Prefixes屬性,以指定HTTP伺服端的IP位址及通訊埠
                httpListener.Prefixes.Add(prefix);

                // 等候自用戶端的連線請求
                httpListener.Start();

                //Console.WriteLine("HTTP server started at: " + Dns.GetHostName() + ":" + Port);

                HTTPSession httpSession = new HTTPSession(httpListener);

                // 執行緒
                ThreadStart serverThreadStart = new ThreadStart(httpSession.HTTPSessionThread);
                Thread      serverthread      = new Thread(serverThreadStart);

                serverthread.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            try
            {
                // 建立伺服端Socket
                Socket serverSocket = new Socket(AddressFamily.InterNetwork,
                                                 SocketType.Stream, ProtocolType.Tcp);

                // 取得本機的識別名稱
                string hostname = Dns.GetHostName();

                // 取得主機的DNS資訊
                IPAddress serverIP = Dns.Resolve(hostname).AddressList[0];

                // HTTP Server Port = 80
                string Port = "80";

                // 處理主機IP位址及主機服務所需的通訊埠資訊
                IPEndPoint serverhost = new IPEndPoint(serverIP, Int32.Parse(Port));

                // 繫結設定伺服端Socket
                serverSocket.Bind(serverhost);

                // 開始接聽等候用戶端的網路連線請求
                // 設定伺服端最大用戶端連線數為int.MaxValue
                serverSocket.Listen(int.MaxValue);

                Console.WriteLine("HTTP server started at: " + serverhost.Address.ToString() + ":" + Port);

                HTTPSession httpSession = new HTTPSession(serverSocket);

                // 執行緒
                ThreadStart serverThreadStart = new ThreadStart(httpSession.HTTPSessionThread);
                Thread      serverthread      = new Thread(serverThreadStart);

                serverthread.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }
        }