各位大虾,我在网上找了一段程序,用来控制只运行一个实例,
发觉只要用telnet之类的一连接到该端口,程序就退出了,
然后我加了点代码用来发送-stop字符串,然后接受处判断是否正确才决定退出实例,问题:
运行A
运行B(能把A给退出)
运行C(不能把B退出)各位帮我看看怎么回事,谢谢
public void run() {
PrintWriter out;
try{
Socket sock = new Socket("127.0.0.1",22345);
//创建socket,连接22222端口/*
网上找的代码里没有这些的,我加了发送-stop用来避免随便一个telnet程序已连接到该端口就会把程序退出
*/
out = new PrintWriter(sock.getOutputStream(),true);
BufferedReader line = new BufferedReader(new InputStreamReader(System.in));
out.println("-stop");
line.close();
out.close();
sock.close();
/////////////////////////// }
catch (Exception e)
{
e.printStackTrace();
}
try{
ServerSocket server = new ServerSocket(22345);//创建socket,在22222端口监听
boolean blnContinue =true;
while(blnContinue)
{
socket = server.accept(); //等待连接 /*
网上找的代码里没有这些的,我加了发送-stop用来避免随便一个telnet程序已连接到该端口
就会把程序退出
*/
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = in.readLine();
System.out.println(line);
/////////////////////////// if(line.equals("-stop"))
{
server.close(); //有连接到来,并且发送了-stop,也就是说有新的实例
socket.close();
blnContinue=false;
}
}
System.exit(0); //这个实例退出
}catch (Exception e)
{
e.printStackTrace();
}
}
发觉只要用telnet之类的一连接到该端口,程序就退出了,
然后我加了点代码用来发送-stop字符串,然后接受处判断是否正确才决定退出实例,问题:
运行A
运行B(能把A给退出)
运行C(不能把B退出)各位帮我看看怎么回事,谢谢
public void run() {
PrintWriter out;
try{
Socket sock = new Socket("127.0.0.1",22345);
//创建socket,连接22222端口/*
网上找的代码里没有这些的,我加了发送-stop用来避免随便一个telnet程序已连接到该端口就会把程序退出
*/
out = new PrintWriter(sock.getOutputStream(),true);
BufferedReader line = new BufferedReader(new InputStreamReader(System.in));
out.println("-stop");
line.close();
out.close();
sock.close();
/////////////////////////// }
catch (Exception e)
{
e.printStackTrace();
}
try{
ServerSocket server = new ServerSocket(22345);//创建socket,在22222端口监听
boolean blnContinue =true;
while(blnContinue)
{
socket = server.accept(); //等待连接 /*
网上找的代码里没有这些的,我加了发送-stop用来避免随便一个telnet程序已连接到该端口
就会把程序退出
*/
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = in.readLine();
System.out.println(line);
/////////////////////////// if(line.equals("-stop"))
{
server.close(); //有连接到来,并且发送了-stop,也就是说有新的实例
socket.close();
blnContinue=false;
}
}
System.exit(0); //这个实例退出
}catch (Exception e)
{
e.printStackTrace();
}
}
解决方案 »
- 连接sqlserver 出错
- 请教一个SSL证书的问题,客户端随便用什么证书都能与服务端建立SSL连接
- 一道关于垃圾回收器的题目,答案为什么是这样的阿
- 类似VB中的DataGrid控件那样的组件,是用什么创建。
- 关于jfreereport得问题
- 我写struts有一段时间了,发现几乎就没有用到继承,多态..这些面向对象的技术
- jdk1.5.0装好了,可是还是运行1.4.2。怎么办:-(高手指教
- 再次散分且送100Memail,要的快跟。
- [求助]有关XML文件数字签名验证
- 一个仿微信飞机大战的小问题
- 请问各位高手一个比较简单的问题.
- 因为有大家的积极参与,程序员在深圳QQ群已经到第十群了,达1400人,第十群群号是:17541586
public class InstanceControl2 extends Thread {
public void run() {
}
}
调用:
public class testInstanceControl2 extends Thread {
public static void main(String args[])
{
InstanceControl2 ic = new InstanceControl2();
ic.start(); testInstanceControl2 frame = new testInstanceControl2();
frame.argss=args[0].toString();
frame.start();
}
public void run()
{
for (;;)
{
try
{
System.out.println(argss+ ":" + new Date().toString());
sleep(2000);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}
{
server.close(); //有连接到来,并且发送了-stop,也就是说有新的实例
socket.close();
blnContinue=false;
System.exit(0); //这个实例退出
}
}
//自己写了一个...你看看
import java.io.*;
import java.net.*;
class OnlyOne extends Thread
{
int port=600;//端口
ServerSocket server=null;
OnlyOne()
{start();}
public void run()
{
boolean isHave;//是否存在实例
Socket s=null;
//测试是否存在实例
try{
s=new Socket("127.0.0.1",600);
isHave=true;
}catch(IOException e){isHave=false;//不存在实例}
}
try{
if(isHave&&s!=null)//存在实例
{
OutputStream out=s.getOutputStream();
out.write("-stop".getBytes());//关闭以前的实例
out.close();
s.close();
//现在在自己在实例则启动监听
nowIsMe();
}
else
{
nowIsMe();
}
if(s==null)
nowIsMe();
}catch(IOException e){System.out.println("Error:"+e);return;}
}
//当自己身为实例时,用来启动监听
private void nowIsMe() throws IOException
{
System.out.println("现在我是实例");
server=new ServerSocket(600);//监听端口
while(true)
{
Socket socket=server.accept();
InputStream in=socket.getInputStream();
DataInputStream din=new DataInputStream(in);
String line=din.readLine();
if(line.equals("-stop")){
close();
System.exit(0);//如果是stop则退出
}
din.close();
in.close();
socket.close();
}
}
private void close()
{
try{
if(server!=null)
server.close();
}catch(Exception e){System.out.println(e);}
}
public static void main(String args[])
{
OnlyOne only=new OnlyOne();
}
}