C# 開發一個簡單的HTTP服務器及命令行解釋器


有時在做命令行程序或form程序時,會爲事件的觸發,考慮相應的機制,例如如果是外部條件觸發時,可以有兩種方式觸發

1、通過TCP或UDP方式(優點是可以雙工通信)

2、在服務器通過構建一個HTTP簡單的服務器實現


以下簡單的寫下通過C#構建一個HTTP服務器,做簡單的命令解析,以及開啓另外一個線程,監聽用戶的鍵盤輸入:


 class Program
    { 



        static void Main(string[] args)
        {


            Init();


            //啓動HTTP線程服務器,用於接受HTTP的請求,並做出相應的操作;

            Thread httpThread = new Thread(new HttpListenerUtil().HTTP服務器監聽);

            httpThread.Start();

            

            Thread ListenerCommInput = new Thread(ReadInputComand);

            ListenerCommInput.Start();



            //查詢mongoDB裏的數據();

            //DataMoveClass.遷移地區數據();

            //DataMoveClass.遷移統計表數據();

            //DataMoveClass.數據遷移();

        }




        private static void Init()
        {
            Console.WriteLine("準備遷移數據至mongodb數據庫中!");
            Console.WriteLine("初始化連接aic數據庫 及 mongodb數據庫!");
        }

     

        /// <summary>
        /// 監聽命令行的輸入操作!並做出相應的反饋
        /// </summary>
        public static void ReadInputComand()
        {
            Console.WriteLine("讀取命令行輸入:(輸入help查看命令參數!)");


            while (true)
            {


                string read = Console.ReadLine();


                if (read == "help")

                {

                    Console.WriteLine("\nhelp:\t 幫助命令");

                    Console.WriteLine("start count:\t 更新統計表");

                    Console.WriteLine("start region:\t 更新區域表");

                    Console.WriteLine("start cominfo:\t 更新企業信息表");

                    Console.WriteLine("quit:\t 退出系統");

                    Console.WriteLine("顯示命令行幫助\n");

                }

                else if (read == "start count")

                {

                    Console.WriteLine("準備更新統計表!");

                    DataMoveClass.遷移統計表數據();

                }

                else if (read == "quit")

                {

                    Console.WriteLine("退出系統!");

                    Thread.Sleep(5000);

                    Environment.Exit(0);

                    return;

                }

                else if (read=="start region")

                {                    

                    Console.WriteLine("準備更新地區表!");

                    DataMoveClass.遷移地區數據();

                }

                else if (read == "start cominfo")

                {                    

                    Console.WriteLine("準備更新企業表!");

                    DataMoveClass.數據遷移();

                }

                else 

                {

                    Console.WriteLine("不識別的命令!\n輸入 help查看幫助\n");

                }



            }



        }




        /// <summary>
        /// 服務器監聽方法;
        /// </summary>
        public void HTTP服務器監聽()
        {
            HttpListener httpListener = new HttpListener();


            httpListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
            httpListener.Prefixes.Add( getDevice("region"));
            httpListener.Prefixes.Add( getDevice("count"));
            httpListener.Prefixes.Add( getDevice("cominfo"));//該接口用戶查詢設備狀態 




           
            try
            {
                httpListener.Start();
            }
            catch (Exception e)
            {


                Console.WriteLine("開啓http服務器失敗,檢查配置文件IP與本機IP是否一致:" + e.Message);
                return;
            }


             
            Console.WriteLine("HTTP服務器開始運行......");
            while (true)
            { 
                HttpListenerContext ctx = httpListener.GetContext();
                ctx.Response.StatusCode = 200;//設置返回給客服端http狀態代碼
                // string name = ctx.Request.QueryString["mumuid"];


                 
                StreamWriter writer = new StreamWriter(ctx.Response.OutputStream);


                string resString = "";
                try
                {
                    resString=處理分析HTTP請求的詳細列表(ctx);
                }
                catch (Exception ep)
                {


                    resString = ep.Message;
                } 
                 
                writer.WriteLine(resString);
                writer.Close();
                ctx.Response.Close(); 


            }


          //  httpListener.Stop();
        }
         


}



最終的效果如下:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章