Example #1
0
        public static void FuncClient()
        {
            byte[] receiveBuffer = new byte[1024];

            string strIniPath = System.Environment.CurrentDirectory + "\\WatchDog.ini";
            string strLog1    = string.Format("Get ini file path = {0}.", strIniPath);

            gLoger.WriteLog(strLog1);

            string strIp = ConfigerHelper.ReadIniData("WatchDog", "serverIP", "127.0.0.1", strIniPath);

            string strPort = ConfigerHelper.ReadIniData("WatchDog", "listenPort", "8885", strIniPath);

            Console.WriteLine("listen port = {0}", strPort);
            int iClientPort = Convert.ToInt32(strPort);

            IPAddress ip = IPAddress.Parse(strIp);

            Socket clientSoct = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                clientSoct.Connect(new IPEndPoint(ip, iClientPort));
                Console.WriteLine("connect sever {0}success.", clientSoct.RemoteEndPoint.ToString());

                int iSendLen = clientSoct.Send(Encoding.ASCII.GetBytes(string.Format("hello server")));
                Console.WriteLine("send 'hello server' to server, length = {0}", iSendLen);

                int iReceiveLen = clientSoct.Receive(receiveBuffer);
                Console.WriteLine("receive '{0}' to server, length = {1}", Encoding.ASCII.GetString(receiveBuffer, 0, iReceiveLen), iReceiveLen);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                clientSoct.Shutdown(SocketShutdown.Both);
            }
            finally
            {
                clientSoct.Close();
            }
        }
Example #2
0
        public static void FuncWatchDog()
        {
            while (true)
            {
                if (g_mutex.WaitOne(1000))
                {
                    g_iDogFood = (g_iDogFood > 0) ? (g_iDogFood - 1) : 0;

                    if (g_iDogFood == 0)
                    {
                        string strIniPath = System.Environment.CurrentDirectory + "\\WatchDog.ini";
                        Console.WriteLine("{0}.", strIniPath);

                        string strValue = ConfigerHelper.ReadIniData("WatchDog", "batFilePath", "./test.bat", strIniPath);
                        string strLog   = string.Format("watch dog is hungry, restart program {0}", strValue);
                        gLoger.WriteLog(strLog);

                        if (File.Exists(strValue))
                        {
                            //存在文件
                            System.Diagnostics.Process p = new System.Diagnostics.Process();
                            p.StartInfo.FileName = "cmd.exe";
                            string strBat = string.Format("/c {0}", strValue);
                            p.StartInfo.Arguments       = strBat;
                            p.StartInfo.UseShellExecute = false;
                            p.StartInfo.CreateNoWindow  = true;
                            p.Start();
                        }
                        else
                        {
                            string strLog2 = string.Format("program {0} is not exisit, start failed.", strValue);
                            gLoger.WriteLog(strLog2);
                        }
                        g_iDogFood = g_iMaxFood;
                    }

                    g_mutex.ReleaseMutex();
                }
                Thread.Sleep(1000);
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            string strIniPath = System.Environment.CurrentDirectory + "\\WatchDog.ini";
            string strLog1    = string.Format("Get ini file path = {0}.", strIniPath);

            gLoger.WriteLog(strLog1);

            string strPort = ConfigerHelper.ReadIniData("WatchDog", "listenPort", "8885", strIniPath);

            Console.WriteLine("listen port = {0}", strPort);
            myProt = Convert.ToInt32(strPort);

            string strValue = ConfigerHelper.ReadIniData("WatchDog", "MaxFood", "30", strIniPath);
            string strLog   = string.Format("Watch dog start, MaxFood = {0}.", strValue);

            gLoger.WriteLog(strLog);

            g_iMaxFood = Convert.ToInt32(strValue);
            g_iDogFood = g_iMaxFood;

            //看门狗线程
            //Thread watchDogThread = new Thread(FuncWatchDog);
            //watchDogThread.Start();

            //------------------tcp test----------------
            //////创建线程监听
            //Thread listenThread = new Thread(ThreadStartFunc_TcpServer);
            //listenThread.Start();

            //Thread clientThread = new Thread(FuncClient);
            //clientThread.Start();

            //WatchDogClient_tcp dogClient = new WatchDogClient_tcp("127.0.0.1", 8005);
            //dogClient.FeedDog(Encoding.ASCII.GetBytes("client Say Hello"));


            //--------------udp client test--------------
            //WatchDogClient_udp dogClient = new WatchDogClient_udp("127.0.0.1", 8885);
            //dogClient.SendFeedDogMessage(Encoding.ASCII.GetBytes("client Say Hello"));

            //------------udp Server test----------------
            //Thread listenThread = new Thread(ThreadStartFunc_UdpServer);
            //listenThread.Start();

#if (TEST_DEAMON_PROCESS)
            gLoger.WriteLog(string.Format("启动程序,pid={0}", Process.GetCurrentProcess().Id));
            DaemonControler Deamon = new DaemonControler();
            if (!Deamon.isDeamonExist())
            {
                Deamon.StartDeamon();
            }
            gLoger.WriteLog(string.Format("当前进程,pid={0}", Process.GetCurrentProcess().Id));

            if (DaemonControler.GetCurrentProcessSum() != 2)
            {
                gLoger.WriteLog(string.Format("当前进程数大于2,不重复执行函数,pid={0}", Process.GetCurrentProcess().Id));
                //return;
            }
            else
            {
                //看门狗线程
                Thread watchDogThread = new Thread(FuncWatchDog);
                watchDogThread.Start();

                //------------udp Server test----------------
                Thread listenThread = new Thread(ThreadStartFunc_UdpServer);
                listenThread.Start();
            }
#else
            //看门狗线程
            Thread watchDogThread = new Thread(FuncWatchDog);
            watchDogThread.Start();

            //------------udp Server test----------------
            Thread listenThread = new Thread(ThreadStartFunc_UdpServer);
            listenThread.Start();
#endif

            Console.WriteLine("main end.pid{0}", Process.GetCurrentProcess().Id);
        }