62,614
社区成员
发帖
与我相关
我的任务
分享
/*
我想实现的是带着参数启动这个程序,当带着start的时候检测系统中是否已启动该程序,
当已启动的时候,则这次第二次启动自动关闭,否则就启动该程序。
当带着stop启动该程序的时候检测系统中是否已启动该程序,
当已启动的时候,这关闭已启动的程序,否则该次启动自动关闭。
*/
/*
你的需求应该是同一时间只允许你程序的一个实例在系统中运行吧。
程序逻辑:
程序启动
开启一个单独线程在某一个端口监听的 ServerSocket
如果捕获到端口已被占用异常则
如果带 start 参数启动,System.exit(0);
如果带 stop 参数启动
连接到另一实例的 ServerSocket 并给它发关闭指令使其退出
否则 System.exit(0);
否则
当监听端口收到退出指令时 System.exit(0);
*/
import java.io.*;
import java.net.*;
public class SingleProcess {
public static void main(final String[] args) {
System.out.println("SingleProcess Start !"); //For Debug
final int PORT = 65530; //监听端口
final byte EXIT = 119; //关闭指令
Thread sst = new Thread(
new Runnable() {
public void run() {
ServerSocket ss = null;
try {
ss = new ServerSocket(PORT, 1);
ss.setSoTimeout(0);
Socket s = null;
InputStream is = null;
while (true) {
try {
s = ss.accept();
is = s.getInputStream();
int cmd = is.read();
if (cmd == EXIT) System.exit(0);
} catch (IOException ex) {
//ex.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ioe) {
//ioe.printStackTrace();
} finally {
is = null;
}
}
if (s != null) {
try {
s.close();
} catch (IOException ioe) {
//ioe.printStackTrace();
} finally {
s = null;
}
}
}
}
} catch (IOException e) {
//e.printStackTrace();
if (args.length <= 0) System.exit(0);
if ("start".equalsIgnoreCase(args[0])) System.exit(0);
if ("stop".equalsIgnoreCase(args[0])) {
Socket s = null;
OutputStream os = null;
try {
s = new Socket((String)null, PORT);
os = s.getOutputStream();
os.write(EXIT); //发送关闭指令
os.flush();
} catch (UnknownHostException ex) {
//ex.printStackTrace();
System.exit(0);
} catch (IOException ex) {
//ex.printStackTrace();
System.exit(0);
} finally {
if (os != null) {
try {
os.close();
} catch (IOException expt) {
//expt.printStackTrace();
} finally {
os = null;
}
}
if (s != null) {
try {
s.close();
} catch (IOException exp) {
//exp.printStackTrace();
} finally {
s = null;
}
}
}
try {
Thread t = (Thread) this.clone();
t.setDaemon(true);
t.start();
} catch (CloneNotSupportedException ec) {
//ec.printStackTrace();
}
}
}
}
}
);
sst.setDaemon(true);
sst.start();
//----------------------------------------------------------------
try {
Thread.sleep(1000 * 6000); //模拟运行耗时
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("SingleProcess End !"); //For Debug
}
}
import java.net.*;
import java.io.*;
public class simpleServer
{
private static ServerSocket serverSocket;
private static listenClient listen;
public static void main(String[] args) throws Exception
{
int port;
if(args.length==0)
{
System.out.println("Usage:java simpleServer [port]");
System.exit(1);
}
port=Integer.parseInt(args[0]);
startServer(port);
}
public static void startServer(int port)throws Exception
{
try
{
serverSocket=new ServerSocket(port);
Thread thread=new Thread(new listenClient(serverSocket));
thread.start();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
class listenClient implements Runnable
{
private ServerSocket serverSocket;
private Socket clientSocket;
DataInputStream in;
DataOutputStream out;
public listenClient(ServerSocket serverSocket)throws Exception
{
this.serverSocket=serverSocket;
}
public void run()
{
try
{
while(true)
{
clientSocket=serverSocket.accept();
System.out.println("Connection from "+clientSocket.getInetAddress().getHostAddress());
in=new DataInputStream(clientSocket.getInputStream());
out=new DataOutputStream(clientSocket.getOutputStream());
String lineSep=System.getProperty("line.separator");//行分隔符,即回车换行
InetAddress addr=serverSocket.getInetAddress().getLocalHost();
String outData="Server Information: "+lineSep+" Local Host: "+serverSocket.getInetAddress().getLocalHost()+lineSep+" port :"+serverSocket.getLocalPort();
byte[] outByte=outData.getBytes();
out.write(outByte,0,outByte.length);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
};