第一步
- 在服务器上配置jre环境。
- 去oracle官网下载 ,然后在环境变量里添加jre路径。
第二步
- 我使用的是PHPStudy集成环境
- 注册apache 和 mysql服务
#切换到相对应的目录 #apacheapache httpd -k install -n "服务名"#mysqlmysqld --install 服务名复制代码
- 写一个启动服务start .cmd
cd /d C:\phpStudy\PHPTutorial\Apache\binnet start apachecd /d C:\phpstudy\PHPTutorial\MySQL\binnet start mysql复制代码
- 需要写一个重启电脑的closeSystem.cmd
shutdown -r -t 1复制代码
第三步
- 把start.cmd放到注册表的启动项中实现开机启动
- cmd输入以下命令 /v 后面是注册名 随意 路径改成相应的
#注册服务启动项 reg add HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run /v add /d C:\regRun\start.cmd /f 复制代码
第四步
获取服务器信息类
package com.boolib.service;import java.net.InetAddress;import java.net.UnknownHostException;import java.text.DecimalFormat;import java.util.Properties;public class MySystemInfo { private String osName; private String osArch; private String osVersion; private String address; private int totalMemoryJVM; private int idleMemoryJVM; private int totalMemory; private int maxMemory; private int idleMemory; private String use; InetAddress address1; Properties pro; String info; public MySystemInfo() { try { pro = System.getProperties(); address1 = InetAddress.getLocalHost(); this.osName = pro.getProperty("os.name"); this.osArch = pro.getProperty("os.arch"); this.osVersion = pro.getProperty("os.version"); this.address = address1.getLocalHost().toString(); this.totalMemoryJVM = (int)Runtime.getRuntime().totalMemory()/1024; this.maxMemory = (int)Runtime.getRuntime().maxMemory()/1024; } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public int getIdleMemoryJVM() { return idleMemoryJVM; } public void setIdleMemoryJVM(int idleMemoryJVM) { this.idleMemoryJVM = idleMemoryJVM; } public String getUse() { return use; } public void setUse(String use) { this.use = use; } //获取服务器信息 public MySystemInfo getInfo() { setIdleMemoryJVM( (int)Runtime.getRuntime().freeMemory()/1024); float d = (1-(float)this.idleMemoryJVM / (float)this.totalMemoryJVM)*100; DecimalFormat decimalFormat=new DecimalFormat(".00"); use=decimalFormat.format(d)+"%"; return this; } @Override public String toString() { return "服务器名称=" + osName + ", 服务器架构=" + osArch + ", 服务器版本号=" + osVersion + ", IP地址=" + address.substring(address.lastIndexOf('/')+1) + ", JVM空闲内存=" + idleMemoryJVM + ", JVM总内存=" + totalMemoryJVM +", 使用率"+use+"\n" ; }}复制代码
信息输出线程类
package com.boolib.service;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.Reader;import java.net.ServerSocket;import java.net.Socket;public class WriteSocket extends Thread{ ServerSocket ssocket; Socket socket; BufferedReader reader; BufferedWriter writer; final int PORT=65530; MySystemInfo mInfo ; String flag; public WriteSocket() { try { this.ssocket = new ServerSocket(PORT); this.mInfo= new MySystemInfo(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run() { try { this.socket = ssocket.accept(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } while(true) { try { writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); writer.write(mInfo.getInfo().toString()); writer.flush(); Thread.sleep(5000); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }} } }复制代码
信息接收线程类
package com.boolib.service;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.ServerSocket;import java.net.Socket;public class ReadSocket extends Thread { BufferedReader reader; String flag; Socket socket; ServerSocket ssocket; final String cmd="C:\\closeSystem.cmd.lnk"; public ReadSocket() { try { this.ssocket = new ServerSocket(65531); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run() { try { socket= ssocket.accept(); int i = 0 ; while(true) { i++; reader = new BufferedReader(new InputStreamReader(socket.getInputStream())) ; System.out.println("当前第"+i+"次接收"); if((flag=reader.readLine())!=null) { if(flag.equals("shutdown")) { ProcessBuilder pb = new ProcessBuilder("cmd","/c",cmd); Process process = pb.start(); //Runtime.getRuntime().exec("C:\\Users\\xu\\Desktop\\stop.cmd.lnk"); System.out.println("命中红心"); break; } } Thread.sleep(500); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }复制代码
这里我设置的服务器监听端口是tcp :65530,65531,需要在服务器上安全组上配置
启动类
package com.boolib.service;public class Start { public static void main(String[] args) { new ReadSocket().start(); new WriteSocket().start(); }}复制代码
生成jar包,传送至服务器。写启动项
#注册监听项reg add HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run /v socket /d C:\regRun\utils\socketjar.cmd /f复制代码
测试 可以用telnet ip 端口号 获取writer
服务器设置成自动登录
搜索 :control userpasswords2;
测试自动重启类
package com.boolib.test;import java.io.BufferedWriter;import java.io.IOException;import java.io.OutputStreamWriter;import java.net.Socket;public class test { public static void main(String[] args) { String path = "你的域名或者ip"; try { Socket so =new Socket(path, 65531); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(so.getOutputStream())); bw.write("shutdown"); bw.write("\n"); bw.flush(); so.shutdownOutput();//此处必须写 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}复制代码