C# 使用Http Post + SharpPcap监听方式处理C/S通讯

来源:互联网 发布:津天元渔具淘宝网 编辑:程序博客网 时间:2024/06/05 19:46

上一篇文章我用Http Post + SharpPcap的方式做了远程启动exe,那么问题来了,我服务端能告诉客户端要做什么,那客户端怎么返回给服务端一个执行状态的通知?如果用WCF来做,是可以很简单处理客户端回调的问题,但是我还没解决那个权限问题,所以还是继续用自己的方式来处理吧。其实我的方法可能很笨,就是再在服务端开一个监听接收客户端通知,由于暂时找不到更好的做法,先实现功能再说吧。

我的做法是:
1. 分别在服务端和客户端配置IIS,并添加网站,端口为81,页面为WebForm1.aspx。IIS的安装如下图
安装IIS

另外还要注册ASP.NET,方法如下:
1. 运行->cmd
2. cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
3. aspnet_regiis.exe -i

  1. 为了使服务端和客户端能通讯,设置防火墙的入站规则
    这里写图片描述

  2. 客户端接收服务端post并回调服务端的代码如下:

/// <summary>        /// 截获包的处理事件        /// Prints the time and length of each received packet        /// </summary>        private void device_OnPacketArrival(object sender, CaptureEventArgs e)        {            //转换为TCP包            var packet = Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);            var tcpPacket = TcpPacket.GetEncapsulated(packet);            //用UTF8编码解析包的内容            var datastr = Encoding.UTF8.GetString(tcpPacket.PayloadData);            //start game            if (datastr == "3")            {                Process[] app = Process.GetProcessesByName(processName);                Console.WriteLine(app.Length);                if (app.Length != 0)                {                    if (HttpPost(postAddress, (int)STATE.正在运行) == 0) { }                    else                    {                        //写网络异常日志                        FailLog(serviceLogPath + "NetWorkFailure.txt", "服务端网络异常,时间:" + DateTime.Now.ToString() + "  事件:启动游戏");                    }                }                else                {                    StartProcess();                    Process[] pro = Process.GetProcessesByName(processName);                    Console.WriteLine(pro.Length);                    if (pro.Length != 0)  //启动成功                    {                        if (HttpPost(postAddress, (int)STATE.启动成功) == 0) { }                        else                        {                            //写网络异常日志                            FailLog(serviceLogPath + "NetWorkFailure.txt", "服务端网络异常,时间:" + DateTime.Now.ToString() + "  事件:启动游戏");                        }                    }                    else  //启动失败                    {                        if (HttpPost(postAddress, (int)STATE.启动失败) == 0) { }                        else                        {                            //写网络异常日志                            FailLog(serviceLogPath + "NetWorkFailure.txt", "服务端网络异常,时间:" + DateTime.Now.ToString() + "  事件:启动游戏");                        }                        FailLog(serviceLogPath + "StartFailure.txt", "游戏启动失败,时间:" + DateTime.Now.ToString());                    }                }            }            //stop game            if (datastr == "4")            {                Process[] app = Process.GetProcessesByName(processName);                if (app.Length != 0)                {                    //停止程序                    foreach (Process p in app)                    {                        if (p.ProcessName == processName)                        {                            p.Kill();                        }                    }                    //确认程序是否已停止                    Thread.Sleep(500);                    Process[] pro = Process.GetProcessesByName(processName);                    if (pro.Length == 0)  //停止成功                    {                        if (HttpPost(postAddress, (int)STATE.停止成功) == 0) { }                        else                        {                            //写网络异常日志                            FailLog(serviceLogPath + "NetWorkFailure.txt", "服务端网络异常,时间:" + DateTime.Now.ToString() + "  事件:停止游戏");                        }                    }                    else  //停止失败                    {                        if (HttpPost(postAddress, (int)STATE.停止失败) == 0) { }                        else                        {                            //写网络异常日志                            FailLog(serviceLogPath + "NetWorkFailure.txt", "服务端网络异常,时间:" + DateTime.Now.ToString() + "  事件:停止游戏");                        }                        FailLog(serviceLogPath + "StopFailure.txt", "游戏停止失败,时间:" + DateTime.Now.ToString());                    }                }                else                {                    if (HttpPost(postAddress, (int)STATE.不在运行) == 0) { }                    else                    {                        //写网络异常日志                        FailLog(serviceLogPath + "NetWorkFailure.txt", "服务端网络异常,时间:" + DateTime.Now.ToString() + "  事件:停止游戏");                    }                }            }        }        public void MyThreadFunc()        {            CreateProcessAsUserWrapper.LaunchChildProcess(processDir);        }        public void StartProcess()        {            System.Threading.Thread ProcessCreationThread = new System.Threading.Thread(MyThreadFunc);            ProcessCreationThread.Start();        }        public int HttpPost(string Url, int PostInfo)        {            try            {                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);                request.Method = "post";                request.ContentType = "application/x-www-form-urlencoded";                request.ContentLength = Encoding.UTF8.GetByteCount(PostInfo.ToString());                //request.CookieContainer = cookie;                Stream myRequestStream = request.GetRequestStream();                StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));                myStreamWriter.Write(PostInfo.ToString());                myStreamWriter.Close();                HttpWebResponse response = (HttpWebResponse)request.GetResponse();                //response.Cookies = cookie.GetCookies(response.ResponseUri);                Stream myResponseStream = response.GetResponseStream();                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));                string retString = myStreamReader.ReadToEnd();                myStreamReader.Close();                myResponseStream.Close();                return 0;            }            catch (Exception e)            {                FailLog(serviceLogPath + "NetWorkFailure.txt", e.ToString());                return -1;            }        }
  1. 上述代码写在Windows服务中,将服务安装好即可
0 0