栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

java监控服务器状态

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java监控服务器状态

packagetest.serv;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.net.InetAddress;importjava.net.InetSocketAddress;importjava.net.ServerSocket;importjava.net.Socket;importjava.net.UnknownHostException;importjava.util.Calendar;importjava.util.Date;importjava.util.regex.Pattern;public classServer {public static voidmain(String[] args) {//启动3000端口

int argnum =args.length;if (argnum < 2) {

System.out.println("请输入IP地址、端口,比如:172.17.201.127 3000");return;

}

String ipaddr= getIP(args[0]);int port = Integer.valueOf(args[1]);

startService(ipaddr,port);}

public static void startService(String ipaddr,intport){

ServerSocket server;

BufferedReader br= null;try{

server= newServerSocket();

server.setReuseAddress(true);

server.bind(newInetSocketAddress(ipaddr,port));

Socket sk= null;

String context= "---服务器端开始接收消息-----";

String filePath= "./log/"+getlognamebyweek();boolean isAppend = true;

String encoding= "UTF-8";while(true){

sk=server.accept();

context="nr";

writeStringToFile(context, filePath, isAppend, encoding);

br= newBufferedReader(newInputStreamReader(sk.getInputStream()));

context=br.readLine();

writeStringToFile(context, filePath, isAppend, encoding);

br.close();

context="nrnr";

writeStringToFile(context, filePath, isAppend, encoding);

}

}catch(IOException e){

System.out.println(e);

}finally{if (br!=null){try{

br.close();

}catch(IOException e) {//TODO 自动生成的 catch 块

e.printStackTrace();

}

}

}

}public static void writeStringToFile(String context, String filePath, booleanisAppend, String encoding)

{

BufferedWriter bfw= null;try{

bfw= new BufferedWriter(new OutputStreamWriter(newFileOutputStream(filePath, isAppend), encoding));

bfw.write(context);

bfw.flush();

bfw.close();

}catch(IOException localIOException1)

{try{if (bfw != null)

bfw.close();

}catch(IOException e)

{

e.printStackTrace();

}

}finally{try{if (bfw != null)

bfw.close();

}catch(IOException e)

{

e.printStackTrace();

}

}

}

public staticString getIP(String name) {

String pattern= "^[0-9]+.[0-9]+.[0-9]+.[0-9]+$";boolean isIP =Pattern.matches(pattern, name);if(isIP) {returnname;

}

InetAddress address= null;try{

address=InetAddress.getByName(name);

}catch(UnknownHostException e) {

e.printStackTrace();

System.out.println("获取失败");

}returnaddress.getHostAddress().toString();

}

public staticString getlognamebyweek(){

Date date=newDate();

Calendar c=Calendar.getInstance();

c.setTime(date);//今天是这个星期的第几天

int week=c.get(Calendar.DAY_OF_WEEK)-1;return week+".log";

}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023833.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号