📄 server.java
字号:
package com.sxit.wap.sncs;
/**
* <p>Title: wap II</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: sxit</p>
* @author not attributable
* @version 1.0
*/
import java.net.*;
import java.io.*;
import org.jdom.input.*;
import org.jdom.output.*;
import org.jdom.*;
public class Server {
//接受网管服务器的请求,对发来的命令解释并传回相应的信息
//运行于每台被监视的机器上
public final static int PORT = 8123;
public Server () {
}
public void start () {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket ( Server.PORT );
Socket clientSocket = null;
while ( true ) {
System.out.print("wait......");
clientSocket = serverSocket.accept ();
System.out.println("Socked created.");
ServiceClient ( clientSocket );
//ServiceClient ( null );
//break;
}
} catch ( Exception e1 ) {
} finally {
try {
if (serverSocket != null)
serverSocket.close ();
} catch (Exception e1) {
}
}
}
public static void ServiceClient ( Socket client ) throws IOException {
//对网管服务器发来的命令进行相应,可以解释3种命令,
//1,ping,
//2,socket命令
//请求cpu等信息
try {
BufferedReader br =
new BufferedReader ( new InputStreamReader ( client.getInputStream () ) );
String list = "" ;
String str = "";
//while ( ( str = br.readLine () ) != null )
//list= list + str ;
System.out.println(list);
list = list.trim();
/*
if ("".equals(list)) {
client.close ();
return ;
}*/
//提取信息,并生成xml文件流
Element root = new Element("list");
Element cpu = new Element("cpu");
System.out.println(new ProcessCommand().getCpuInfo());
cpu.addContent(new ProcessCommand().getCpuInfo() );
root.addContent(cpu);
Element disk = new Element("disk");
System.out.println(new ProcessCommand().getDiskInfo());
disk.addContent(new ProcessCommand().getDiskInfo() );
root.addContent(disk);
org.jdom.Document doc = new org.jdom.Document(root);
org.jdom.output.XMLOutputter xml= new org.jdom.output.XMLOutputter("",true,"gb2312");
xml.output(doc,client.getOutputStream());
xml.output(doc,new FileOutputStream("D:\\test.xml"));
} catch (Exception e1) {
e1.printStackTrace();
}
client.close ();
}
public static void main ( String[] args ) {
Server server = new Server ();
server.start();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -