[小戴]用C#制作监听服务器

废话

        在给一家工作生产线流程程序的制作,这边有一个需求,客户买的产品是一家硬件公司提供的,主要为了生产流水线的计件,现在能实现的功能是在硬件上能显示当前的数量,原先流水线管理者会去每隔一段时间去等级一下数量情况。现在主要做智能制造,把原先的线下的工程全部搬到线上来,需要对这个数据传送到我们制造的ERP上,后来通过各方协调通过c#来做监听服务器,原先的硬件系统来发送实时数据来实现。


环境


       环境:windows server server 2008r2

       开放的服务器端口:503


代码实现


        #region"打开socks监听"
        public static void Init(String ip,int duankou)
        {
            Thread handleSocket = new Thread(new ThreadStart(HandleSocket));
            if (handleSocket != null && handleSocket.IsAlive)
            {
                handleSocket.Abort();
            }
            else
            {
                serviceSocketListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    serviceSocketListener.Bind(new IPEndPoint(IPAddress.Parse(ip), duankou)); //IP和端口应该是可配置
                    serviceSocketListener.Listen(1024);
                    // Thread handleSocket = new Thread(new ThreadStart(HandleSocket));
                    handleSocket.Start();
                }
                catch {
                    serviceSocketListener.Close();
                    serviceSocketListener.Dispose();
                }
            }
        }
        #endregion
        #region"监听链接方法"
        private static void HandleSocket()
        {
            while (true)
            {
                try
                {
                    Socket currSocket = serviceSocketListener.Accept();  //为新建连接创建新的 System.Net.Sockets.Socket
                    Thread processThread = new Thread(new ParameterizedThreadStart(ProcessSocket));
                    processThread.Start(currSocket);
                }
                catch { }
            }
        }
        #endregion
        #region "处理Socket报文"
        public  static void ProcessSocket(object obj)
        {
            Socket currSocket = (Socket)obj;
            Form1 frm = new Form1();
            try
            {
                byte[] recvBytess = new byte[1048576];
                int recbytes;
                recbytes = currSocket.Receive(recvBytess, recvBytess.Length, 0);
                if (recbytes > 0)
                {
                    var contentStr = Encoding.UTF8.GetString(recvBytess, 0, recbytes);
                    var _order = contentStr.Split('~');
                    byte[] sendPass = Encoding.UTF8.GetBytes(_order[0].ToUpper() + "#SUCCESS"); //先相应对话,然后去异步处理
                    currSocket.Send(sendPass, sendPass.Length, SocketFlags.None);
                    switch (_order[0].ToUpper())
                    {
                        case "#13":
                            // frm.OutMsg(frm.txtResult, "[系统消息]****读取报文信息:" + _order[1] + "****"+ frm.time(true), Color.Green);
                            MessageBox.Show(_order[1]);
                            break;
                        default:
                            //frm.OutMsg(frm.txtResult, "[系统消息]****报文出错,出错内容为" + _order[1] + "****"+ frm.time(true), Color.Green);
                            MessageBox.Show(_order[1]);
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                frm.OutMsg(frm.txtResult, "[系统消息]****报文处理内容出错!****"+ frm.time(true), Color.Red);
            }
        }
        #endregion
        #region"执行监听方法"
        private void anniu_Click(object sender, EventArgs e)
        {
            string ipdizhi = ip.Text;
            int duankoudizhi = int.Parse(dkh.Text);
            if (flag == 0)
            {
                groupBox1.Enabled = false;
                anniu.Text = "停止监听";
                flag = 1;
                Control.CheckForIllegalCrossThreadCalls = false;
                OutMsg(txtResult, "[系统消息]****开始执行监听线程****" + time(true), Color.Green);
                try
                {
                    Init(ipdizhi, duankoudizhi);
                    GC.Collect();
                }
                catch (Exception M)
                {
                    OutMsg(txtResult, "[系统消息]****执行发生问题错误信息:" + M + "****" + time(true), Color.Red);
                }
            }
            else
            {

                groupBox1.Enabled = true;
                anniu.Text = "打开监听";
                Init(ipdizhi, duankoudizhi);
                flag = 0;
                OutMsg(txtResult, "[系统消息]****已经停止监听****" + time(true), Color.Green);
            }
        }
        #endregion

结果


微信图片_20170705124608.jpg



Top