总体来说很简单,也没进行什么容错处理,额就酱吧最近忙的一笔,
发上来当作自己的学习笔记好了,也欢迎批评指正,随意喷。
————————————————————————————
PC服务端,监听端口,根据客户端发来的消息进行不同的操作
其中调用了关机程序等。
package pc_service;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Pc_service {
private static ServerSocket serverSocket =null;
private static DataInputStream data_input = null;
public static void main(String[] args) {
// TODO 自动生成的方法存根
try {
serverSocket = new ServerSocket(3333);
System.out.println("listening 3333 port.");
while(true){
System.out.println("wait for connect.");
Socket client_socket = serverSocket.accept();
System.out.println("" + client_socket.getInetAddress());
try{
//获取输入流,读取客户端传来的数据。
data_input = new DataInputStream(client_socket.getInputStream());
String msg = data_input.readUTF();
System.out.println("msg " + msg);
if(msg.equals("shutdown")){
Shutdown();
} else if(msg.equals("restart")){
Restart();
} else if(msg.equals("logoff")){
Logoff();
}
} catch(Exception e){
e.printStackTrace();
} finally {
try{
data_input.close();
client_socket.close();
} catch(IOException e){
e.printStackTrace();
}
}
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
private static void Logoff() throws IOException {
// TODO 自动生成的方法存根
Process p = Runtime.getRuntime().exec("shutdown -l");
System.out.println("logoff, 0 seconds later.");
}
private static void Restart() throws IOException {
// TODO 自动生成的方法存根
Process p = Runtime.getRuntime().exec("shutdown -r -t 5");
System.out.println("restart, 5 seconds later.");
}
private static void Shutdown() throws IOException {
// TODO 自动生成的方法存根
Process p = Runtime.getRuntime().exec("shutdown -s -t 5");
System.out.println("shut down, 5 seconds later.");
}
}
android控制端
首先是界面布局, 这个没什么好说的,在界面上简单的放几个button,用于控制不同的功能。
<span style="font-size:12px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="@layout/title_bar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<EditText
android:id="@+id/input_ip"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="输入电脑的ip地址"
/>
<Button
android:id="@+id/connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
/>
</LinearLayout>
<Button
android:id="@+id/shut_down"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="关机" />
<Button
android:id="@+id/restart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="重启" />
<Button
android:id="@+id/log_off"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注销" />
</LinearLayout></span>
接下来是通过输入的ip地址连接服务器,并处理各按钮点击事件
package com.chuyunxuanyu.shutdown;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private static final int UPDATE_TEXT = 1;
private static ConnectivityManager connectivityManager;
private static NetworkInfo networkInfo;
private Button shutDown;
private Button restart;
private Button logOff;
private Button connect;
private EditText ip;
private TextView show_ip;
private static boolean flag = false;
private static Socket socket;
private DataOutputStream data_output;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch(msg.what){
case UPDATE_TEXT:
Toast.makeText(MainActivity.this, "连接" + (String) msg.obj + "成功", Toast.LENGTH_SHORT).show();
Log.d("hehe", "3 " + (String) msg.obj);
show_ip.setText("已连接到" + (String) msg.obj);
ip.setEnabled(false);
connect.setEnabled(false);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
initListener();
//initData();
}
private void initData() {
// TODO Auto-generated method stub
connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
networkInfo = connectivityManager.getActiveNetworkInfo();
}
private void initListener() {
// TODO Auto-generated method stub
shutDown.setOnClickListener(this);
restart.setOnClickListener(this);
logOff.setOnClickListener(this);
connect.setOnClickListener(this);
}
private void initView() {
// TODO Auto-generated method stub
shutDown = (Button) findViewById(R.id.shut_down);
restart = (Button) findViewById(R.id.restart);
logOff = (Button) findViewById(R.id.log_off);
connect = (Button) findViewById(R.id.connect);
ip = (EditText) findViewById(R.id.input_ip);
show_ip = (TextView) findViewById(R.id.ip);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String text = "";
switch(v.getId()){
case R.id.connect:
final String ip_address = ip.getText().toString();
if(ip_address.equals("")){
Toast.makeText(this, "请输入正确的ip地址", Toast.LENGTH_SHORT).show();
return;
}
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
try {
socket = new Socket(ip_address, 3333);
data_output = new DataOutputStream(socket.getOutputStream());
flag = true;
Log.d("hehe", "1 " + ip_address);
Log.d("hehe", "1.5 ");
Log.d("hehe", "1.6 ");
Message message = new Message();
message.what = UPDATE_TEXT;
message.obj = ip_address;
Log.d("hehe", "2 " + (String) message.obj);
handler.sendMessage(message);
//Log.d("caonima", ip_address);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return;
}
}).start();
return;
case R.id.shut_down:
if(flag){
text = "shutdown";
Log.d("caonima", text);
}
break;
case R.id.restart:
if(flag){
text = "restart";
Log.d("caonima", text);
}
break;
case R.id.log_off:
if(flag){
text = "logoff";
Log.d("caonima", text);
}
break;
}
try{
if((data_output != null) && (!text.equals(""))){
data_output.writeUTF(text);
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
data_output.close();
socket.close();
flag = false;
ip.setEnabled(true);
connect.setEnabled(true);
show_ip.setText("当前没有连接电脑");
}
} catch(Exception e){
e.printStackTrace();
}
}
}