各位大虾,我在网上找了一段程序,用来控制只运行一个实例,
发觉只要用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();
  }
}

解决方案 »

  1.   

    这段就是主要源码了阿。。
    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());
        }
       }
    }
    }
      

  2.   

    if(line.equals("-stop"))
    {
    server.close(); //有连接到来,并且发送了-stop,也就是说有新的实例
    socket.close();
    blnContinue=false;
    System.exit(0); //这个实例退出
    }
    }
      

  3.   

    //不太清楚你写的.不过可能是在,发送了-stop之后没有启支监听,至以于第三次就退不出去!
    //自己写了一个...你看看
    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();
    }
    }