zoukankan      html  css  js  c++  java
  • 开发日记:中控PUSH协议

    using System;
    using System.IO;
    using System.Net;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var listerner = new HttpListener())
                {
                    listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
                    var ipEntry = Dns.GetHostEntry(Dns.GetHostName());
                    var ipList = ipEntry.AddressList;
                    const int port = 8088;
                    foreach (var ip in ipList)
                    {
                        if (IsCorrenctIp(ip.ToString()))
                        {
                            listerner.Prefixes.Add("http://" + ip + ":"+ port + "/iclock/");
                        }
                    }
                    listerner.Prefixes.Add("http://127.0.0.1:"+ port + "/iclock/");
                    listerner.Prefixes.Add("http://localhost:"+ port + "/iclock/");
    
                    listerner.Start();
    
                    Console.WriteLine("【系统提示】考勤管理系统启动成功!");
                    while (true)
                    {
                        //等待请求连接
                        //没有请求则GetContext处于阻塞状态
                        var ctx = listerner.GetContext();
                        ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码
                        var sn = ctx.Request.QueryString["SN"];
                        var httpMethod=ctx.Request.HttpMethod;
                        var table = ctx.Request.QueryString["table"];
                        var count = 1;
    
                        if ((sn != null) &&(table!=null)&&table== "ATTLOG"&&(httpMethod=="POST"))
                        {
                            Console.WriteLine("设备号:"+sn);
                            
                            var result = GetRequestPostData(ctx.Request, out count);
                            var array = result.Split('	');
                            var userId = array[0];
                            var userName = "未知人员";
                            if (userId == "1")
                            {
                                userName = "黄海";
                            }
                            if (userId == "2")
                            {
                                userName = "吴缤";
                            }
                            if (userId == "3")
                            {
                                userName = "申健";
                            }
    
                            if (userId == "197710110")
                            {
                                userName = "周枫";
                            }
    
                            Console.WriteLine(userName + "    " + array[1]);
                        }
                       
                        //使用Writer输出http响应代码
                        using (var writer = new StreamWriter(ctx.Response.OutputStream))
                        {
                            ctx.Response.ContentType = ctx.Request.AcceptTypes[0];
                            writer.WriteLine("HTTP/1.1 200 OK"+"<br>");
                            writer.WriteLine("Server: DsidealSuperServer/1.9.0" + "<br>");
                            writer.WriteLine(DateTime.Now.ToString("r") + "<br>");
                            writer.WriteLine("Content-Type: text/plain" + "<br>");
                            writer.WriteLine("Connection: close" + "<br>");
                            writer.WriteLine("Content-Length: "+(3+count.ToString().Length)+ "<br>");
                            writer.WriteLine("Pragma: no-cache" + "<br>");
                            writer.WriteLine("Cache-Control: no-store" + "<br>");
                            writer.WriteLine("" + "<br>");
                            writer.WriteLine("OK:"+ count + "<br>");
                            writer.Close();
                            ctx.Response.Close();
                        }
                    }
                }
            }
           
            public static bool IsCorrenctIp(string ip)
            {
                var pattrn = @"(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])";
                return Regex.IsMatch(ip, pattrn);
            }
            public static string GetRequestPostData(HttpListenerRequest request,out int Count)
            {
                Count = 0;
                if (!request.HasEntityBody)
                {
                    return null;
                }
                var returnStr = "";
              
                using (var body = request.InputStream)
                {
                    using (var reader = new StreamReader(body, request.ContentEncoding))
                    {
                        while (reader.Peek() >= 0)
                        {
                            var nowString = (char) reader.Read();
                            if (nowString.ToString() == "
    ")
                            {
                                Count++;
                            }
                            returnStr = returnStr+ nowString;
                        }
                    }
                }
                return returnStr;
            }
    
        }
    }
  • 相关阅读:
    JavaScript根据CSS的Media Queries来判断浏览设备的方法
    JavaScript API 设计原则
    高性能 CSS3 动画
    CSS代码实例:用CSS代码写出的各种形状图形
    frontpage 2010.2003绿色版
    Web前端年后跳槽必看的各种面试题
    [ksm][数学] Jzoj P5810 简单的玄学
    [分治] Jzoj P5807 简单的区间
    [dfs][bfs] Jzoj P5806 简单的操作
    [dp] Jzoj P5804 简单的序列
  • 原文地址:https://www.cnblogs.com/luomingui/p/12143529.html
Copyright © 2011-2022 走看看