手机控制电脑关机程序

原理:移动端与服务器端通过Socket建立连接并通信。

服务器端:

package shutdownWithAndroid;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {

    static void shutdown(){

          try {  
                  Runtime.getRuntime().exec("shutdown -s -t 10");  

              }catch (Exception e) {  

                  e.printStackTrace();

         }                                              
    }     

    public static void main(String[] args) throws IOException{

        ServerSocket  ss = new ServerSocket(30000);

        while (true){
            BufferedReader reader =null;
            InputStreamReader isr =null;
            InputStream is =null;

            PrintStream ps =null;
            OutputStream os =null;

            Socket s = ss.accept();

            is = s.getInputStream();                                                    
            os = s.getOutputStream();

            if(is!=null){
                isr = new InputStreamReader(is);
                reader = new BufferedReader(isr);
                String line = reader.readLine();
                System.out.println(line);

                if(line!=null){
                    if(os!=null){
                        ps = new PrintStream(os);
                        ps.print("true");
// shutdown();
                        ps.close(); 
                        System.out.println("****************************");
                        }
                    }   
                reader.close();
                }
            s.close();
            }

    }

}

移动端:

package com.example.shutdownpc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity {

    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn = (Button) findViewById(R.id.shutdown);

        btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            new Thread() 
            //里面没有Runnable
            {
                public void run() {
                    try {           
                        Socket sc = new Socket("192.168.23.5" , 30000); 
                        //换成自己服务器端的IP地址

                        BufferedReader reader =null;
                        InputStreamReader isr =null;
                        InputStream is = null;

                        PrintStream ps =null;
                        OutputStream os =null;                                      

                        os = sc.getOutputStream();
                        is = sc.getInputStream();

                        isr = new InputStreamReader(is);
                        reader = new BufferedReader(isr);

                            if(os!=null){
                                ps = new PrintStream(os);
                                ps.print("hello!"); 
                                ps.close(); 
                                }

// if(reader!=null){ 
// String line = reader.readLine();
// 
// System.out.println(line);
// 
// System.out.println("****************************");
// 
// reader.close(); 
// } 

                            sc.close();                                                     

                    } catch (IOException e) {

                        e.printStackTrace();
                        System.out.println("IOException");
                    }

                }
                }.start();

        }
        });

  }  

}

你可能感兴趣的:(socket,服务器,移动,手机)