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();
        }
         


}



最终的效果如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值