|
|
7楼

楼主 |
发表于 2006-11-24 09:39:13
|
只看该作者
|
自己写个了烂程序,对端口25监控,这下不怕了。传到服务器去试试。经常端口不能使用,烦死了。
---------------------------------------
import java.io.*;
import java.net.*;
import java.io.DataInputStream;
import java.text.SimpleDateFormat;
public class Test {
public static int a=20; //程序重启等待间隔时间(秒)
public static int b=60;//监控端口间隔时间(秒)
public static void main(String[] args) {
InputStream Is;
DataInputStream dis;
int port=25;
int s=0;
System.out.println("监控软件已启动(监控端口"+port+")...");
Socket sock = null;
while(true)
{
try {
s=0;
InetAddress addr = InetAddress.getByName(args[0]);
SocketAddress sockaddr = new InetSocketAddress(addr,port);
sock = new Socket();
int timeoutMs = 3000; // 连接端口等待3秒
sock.connect(sockaddr, timeoutMs);
Is=sock.getInputStream();
dis=new DataInputStream(Is);
s=dis.read();
dis.close();
Is.close();
sock.close();
}catch (UnknownHostException e){
System.out.println("NO Found Server");
}catch (SocketTimeoutException e){
System.out.println("SocketTimeout");
}catch (IOException e){
System.out.println("-----------------------------------");
}finally{
try{
if(sock!= null){
sock.close();
}
}catch(Throwable t){
}
}
try {
if (s!=50){
StartServer();
}
}catch (IOException e){
System.out.println("重启出现IO错误");
}
try{
Thread.sleep(b*1000);
}catch(InterruptedException ex){
System.out.println("监控端口间隔时间出现错误");
}
}
// System.out.println("监控软件已停止服务....");
}
public static void StartServer() throws IOException
{
SimpleDateFormat tdate= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("服务正在重启中..."+(String)tdate.format(new java.util.Date()));
Process process1 = Runtime.getRuntime().exec("net stop WinWebMail");
try{
Thread.sleep(a*1000);
}catch(InterruptedException ex){
System.out.println("程序重启等待间隔时间出现错误");
}
Process process2 = Runtime.getRuntime().exec("net start WinWebMail");
System.out.println("服务重启完成..."+(String)tdate.format(new java.util.Date()));
}
}
[ 本帖最后由 shixingdong 于 2006-11-24 10:29 编辑 ] |
|