Phone control computer shutdown

Development Background:

  • Children playing computer games, I use the computer
  • Wife watching TV with the computer, I use the computer (hey, shut down the computer how ah, it seems that you do not want to use)
  • Would have been prepared for a while and then roll up the code, the results of bed do not want to play down the phone, so ..

There is already a lot of software to achieve this function, why write your own?

    Just wanted to write, there is source code to feel safe, not so many other features, in addition to facilitate their expansion, such as uploading child phone location information, something not saved to a third-party server

Why do not Http, Http protocol communication via API interface defined two do not have to?

     Boot to start Tomcat need to consume dozens of M memory, and I need to duplex, said this after

Why not set up boot and application services using the Window?

     Do not be afraid to whitelist 360 to prompt it embarrassing, the other startup items inside to see is not good, and the service contents inside more, most people do not bother to look at, more stable service

Functional Description:

Server:

  Boot, sent from the phone to receive messages processed according to category

Client:

  Determining a cancel shutdown and shutdown button, a text box to set the delay time of
  a program icon display whether the normal connection to the server, refresh 2 seconds

  Set up a server with IP interface ports and can be saved;

 

Others can make use of them:

  1. Logging Logger
  2. Window service helper are common (install uninstall inspection services)
  3.  Shutdown batch CmdHelper
  4. You add an application to the firewall whitelist (Note that this path, I am here a few laps around)

 

Operating environment and use:

  • PC: C # .netframework 4.6.1 may need to drop administrator privileges

The first step: opening the Release folder below PhoneControl.exe.config modify the <add key = "IPEndpoint" value = "192.168.3.16:1337" />, the value into your own

Step two: Click Run Release folder below PhoneControl.exe, the first installation of 1 server operation ends (I set the delay time required to wait for 10 seconds to complete the installation, not because the program is running slow, delay settings fancy article)

  • Android: Java API 15 Android 4.0.3 above requires network permissions

 To the following address installation phonecontrol-debug.apk, completed there will be a very primitive icon on the screen and then, MainActivty, appears below:

The first step: first server IP and port fill up, click after setting

Step Two: After modifying how long the shutdown, and click on the shutdown button

 

Application: If not the normal operation the error message sent to the comments inside

Server Download:

Android Download:

 Source show:

Server:

  ///  <Summary> 
        /// main entry point for the application.
        ///  </ Summary> 
        static  void the Main ( String [] args) 
        { 
            // determine whether existing logging directory if the directory is present in the first run as different logging run directory service will exist Window   
            String l_strLogPath = 
                the System .Configuration.ConfigurationManager.AppSettings [ " the LogPath " ];
             IF ( String .IsNullOrEmpty (l_strLogPath)) 
            { 
                // record the program in the current directory 
                the Configuration CFA = " 
                    ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None);
                cfa.AppSettings.Settings [ " the LogPath ] .Value = Environment.CurrentDirectory; 
                cfa.Save (); 
            } 


            // add the application to the directory list of firewall rules inside 
            the try 
            { 
                FireWareHelper fireWareHelper = new new FireWareHelper ();
                 // String l_strPath the Path = .Combine (Environment.CurrentDirectory, "PhoneControl.exe"); 
                fireWareHelper.RemoveRule (Application.ExecutablePath);
                 // directory can only be used here rather than using Application.ExecutablePath though l_strPath when you output the same two addresses to Window service which is not the same 
                fireWareHelper.AddRule (Application.ExecutablePath," PhoneControl " ); 
            }
            the catch (Exception E) 
            { 
                Logger.CreateErrorLog (E); 
            } 


#if the DEBUG 
            Logger.WriteAndShowLog ( " program start " ); 
            StartClass.StartMain (); 
            return ;
 #endif 


            String l_strServiceName = 
                System.Configuration.ConfigurationManager.AppSettings [ " the ServiceName " ];
             // parameterized to start running the service 
            IF (args.length> 0 ) 
            { 
                the try 
                { 
                    the ServiceBase [] serviceToRun= New new the ServiceBase [] { new new WindowService ()}; 
                    ServiceBase.Run (serviceToRun); 
                } 
                the catch (Exception EX) 
                { 
                    Logger.CreateErrorLog (EX); 
                } 
            } 
            // no parameters configuration program is started 
            the else 
            { 
                StartLable: 
                Console.WriteLine ( " select an action you want to perform --1: Automated deployment services, 2: installation services, 3: uninstall service, 4: verify the service status, 5: exit " ); 
                Console.WriteLine ( " ------ -------------- "  );
                ConsoleKey Key = Console.ReadKey().Key;

                if (key == ConsoleKey.NumPad1 || key == ConsoleKey.D1)
                {
                    if (ServiceHelper.IsServiceExisted(l_strServiceName))
                    {
                        ServiceHelper.ConfigService(l_strServiceName, false);
                    }

                    if (!ServiceHelper.IsServiceExisted(l_strServiceName))
                    {
                        ServiceHelper.ConfigService(l_strServiceName, true);
                    }

                    ServiceHelper.StartService(l_strServiceName);
                    goto StartLable;
                }
                else if (key == ConsoleKey.NumPad2 || key == ConsoleKey.D2)
                {
                    if (!ServiceHelper.IsServiceExisted(l_strServiceName))
                    {
                        ServiceHelper.ConfigService(l_strServiceName, true);
                    }
                    else
                    {
                        Console.WriteLine("\n服务已存在......");
                    }

                    goto StartLable;
                }
                else if (key == ConsoleKey.NumPad3 || key == ConsoleKey.D3)
                {
                    if (ServiceHelper.IsServiceExisted(l_strServiceName))
                    {
                        ServiceHelper.ConfigService(l_strServiceName, false);
                    }
                    else
                    {
                        Console.WriteLine("\n服务不存在......");
                    }

                    goto StartLable;
                }
                else if (key == ConsoleKey.NumPad4 || key == ConsoleKey.D4)
                {
                    if (!ServiceHelper.IsServiceExisted(l_strServiceName))
                    { 
                        Console.WriteLine ( " \ n-service does not exist ......" ); 
                    } 
                    The else 
                    { 
                        Console.WriteLine ( " \ n-service state: " + ServiceHelper.GetServiceStatus (l_strServiceName) .ToString ()); 
                    } 

                    GOTO StartLable; 
                } 
                the else  IF (ConsoleKey.NumPad5 || Key Key == == ConsoleKey .D5) 
                { 
                } 
                the else 
                { 
                    Console.WriteLine ( " \ n-enter a valid key! " ); 
                    Console.WriteLine ( " --------------------");
                    goto StartLable;
                }
            }
        }
 public static void StartMain()
        {
            try
            {
                string amqEndpoint = System.Configuration.ConfigurationManager.AppSettings["IPEndpoint"].ToString();

                System.Net.IPAddress IPadr = System.Net.IPAddress.Parse(amqEndpoint.Split(':')[0]);

                System.Net.IPEndPoint
                    EndPoint = new System.Net.IPEndPoint(IPadr,
                        int.Parse(amqEndpoint.Split(':')[1])); //传递IPAddress和Port


                using (var listener = new SocketListener(EndPoint)) // Start listening
                {
                    Logger.WriteAndShowLog("启动监听.....");

                    for (;;)
                    {
                        using (var remote = listener.Accept()) // Accepts a connection (blocks execution)
                        {
                            var data = remote.Receive(); // Receives data (blocks execution)
                            Method, (Data);
                             // remote.Send ( "command has been executed!"); 
                        } 
                    } 
                } 
            } 
            The catch (System.Exception EX) 
            { 
                Logger.CreateErrorLog (EX); 
                Console.ReadLine (); 
            } 
        }
View Code

 

Android client:

 public  void btn_CloseComputer (View View) {
         // Get IP port information with
         // the information displayed on the screen to 
        the SharedPreferences sPreferences = getSharedPreferences ( "config" , MODE_PRIVATE);
          Final    String l_strIp = sPreferences.getString (ConstantModel.ip, "" ) ;
         Final String l_strEndPoint = sPreferences.getString (ConstantModel.endPoint, "1337" ); 

        new new the Thread () { 
            @Override 
            public  void RUN () {
                 Super .run ();
                 the try {
                     // 1. create a listener specify the server address and specify server listening port number
                    Socket = the Socket new new the Socket (l_strIp, the Integer.parseInt (l_strEndPoint));
                     // 2. get the target client socket output stream of data transmitted to the server 
                    the OutputStream OS = Socket.getOutputStream ();
                     // write to be transmitted to the server data 
                    MessageModel Model = new new MessageModel (); 
                    model.setMessageType ( . 1 );
                     // Get the time 
                    the EditText EditText = the findViewById (R.id.txt_Seconds); 
                    String Message = editText.getText () toString ().;

                    model.setDataContent (Message); 
                    Gson GSON = new new Gson (); 
                    String jsonObject = gson.toJson (Model); 

                    String S1 = new new String (jsonObject.getBytes (), StandardCharsets.UTF_8); 
                    os.write (s1.getBytes ( )); 
                    os.flush (); 
                    socket.shutdownOutput (); 
                    // get the input stream of the socket, is used to store the data returned by the server 
                    the InputStream iS = Socket.getInputStream ();
                     // parse the data server returns 
                    int lenght 0 = ;
                     byte[] buff = new byte[1024];
                    final StringBuffer sb = new StringBuffer();
                    while ((lenght = is.read(buff)) != -1) {
                        sb.append(new String(buff, 0, lenght, StandardCharsets.UTF_8));
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //这里更新UI
                        }
                    });
                    //3, close IO resources (note: the actual development of the needs in the finally) 
                    is.close (); 
                    os.close (); 
                    the Socket.close (); 
                } the catch (UnknownHostException E) { 
                    e.printStackTrace (); 
                } the catch (IOException E) { 
                    e.printStackTrace (); 
                } 
            } 
        } .start (); 
    }
public  void btn_Setting (View View) {
         / ** 
         * the SharedPreferences user data stored in the shared_prefs / config.xml file in the package, 
         * and set the read mode of the file is private, i.e., the software itself may only access the file 
         * / 
        SharedPreferences sPreferences = the this .getSharedPreferences ( "config" , MODE_PRIVATE); 
        SharedPreferences.Editor Editor = sPreferences.edit ();
         // course sharepreference will escape some special characters, so that when a more accurate reading 

        txt_EndPoint the TextView = the findViewById (R.id.txt_Port); 
        String l_strPort =  txt_EndPoint.getText () toString ().;
        editor.putString (ConstantModel.endPoint, l_strPort);

        IPEditText ipEditText = findViewById(R.id.ip_text);
        String IP = ipEditText.getText(this);
        editor.putString(ConstantModel.ip, IP);
        editor.commit();

        Toast.makeText(this, "设置成功!", Toast.LENGTH_LONG)
                .show();
    }

 

Source Downloads:

Upload size restrictions, while I upload to Baidu Cloud

 

Guess you like

Origin www.cnblogs.com/maanshancss/p/11537469.html