2017-08-28 99 views
0

你好我正在編寫一個簡單的telnet(後來即將有一個自定義的telnet客戶端解釋命令)命令和控制服務器在純java 和我有一些代碼,只有當我有調試器並在正確的地方斷點這是奇怪爲什麼這段代碼只能在帶有斷點的調試模式下工作? (IDE調試器)

代碼:

主營:

package LiteDoor; 

import java.io.File; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.util.ArrayList; 

import LiteDoor.net.AcceptTelnetClient; 

public class Main { 

    public static final String VERSION = "v0.0.1"; 

    public static File scripts = new File("scripts"); 

    public static ArrayList<AcceptTelnetClient> conns = new ArrayList<AcceptTelnetClient>(); 

    public static void main(String args[]) throws Exception 
    { 
     File autorun = new File(scripts.getAbsolutePath()+File.separatorChar+"autorun.litescript"); 
     if (!autorun.exists()) { 
      scripts.mkdirs(); 
      autorun.createNewFile(); 
     } 

     ServerSocket Soc=new ServerSocket(666); 
     while(true) 
     { 
      Socket CSoc=Soc.accept(); 
      AcceptTelnetClient ob=new AcceptTelnetClient(CSoc); 
     } 
    } 

} 

AcceptTelnetClient:

package LiteDoor.net; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.Socket; 

import LiteDoor.Main; 
import LiteDoor.io.MyBufferedWriter; 

public class AcceptTelnetClient extends Thread { 
    Socket ClientSocket; 
    public boolean master; 
    public String slaveCmd = null; 

    public AcceptTelnetClient(Socket CSoc) throws Exception { 
     Main.conns.add(this); 

     ClientSocket=CSoc; 
     System.out.println("Client Connected ..."); 
     BufferedReader in = new BufferedReader(new InputStreamReader(ClientSocket.getInputStream())); 
     @SuppressWarnings("resource") 
     MyBufferedWriter out = new MyBufferedWriter(new OutputStreamWriter(ClientSocket.getOutputStream())); 

     out.writeln("MASTER/SLAVE?"); 

     switch(in.readLine().toUpperCase()) { 
     case("MASTER"): 
      master = true; 
      break; 
     case("SLAVE"): 
      master = false; 
      break; 
     } 

     int i=0; 
     while(i<200) { 
      out.newLine(); 
      i++; 
     } 
     out.flush(); 

     start();   
    } 

    public void run() { 
     try {  
      BufferedReader in=new BufferedReader(new InputStreamReader(ClientSocket.getInputStream())); 
      @SuppressWarnings("resource") 
      MyBufferedWriter out = new MyBufferedWriter(new OutputStreamWriter(ClientSocket.getOutputStream())); 

      out.writeln(""); 

      if (!master) { 
       BufferedReader fr = new BufferedReader(new FileReader(Main.scripts.getAbsolutePath()+File.separatorChar+"autorun.litescript")); 
       String line = fr.readLine(); 
       while(line != null) { 
        if (!line.startsWith("#") && line != "") { 
         out.writeln(line); 
        } 
        line = fr.readLine(); 
       } 
       fr.close(); 
      } 

      boolean loop = true; 

      while(loop) { 
       if (master) { 
        String cmd = in.readLine(); 
        if(cmd.startsWith("quit")) { 

         loop=false; 

        } else { 
         System.out.println("Master says: " + cmd); 
         for (AcceptTelnetClient con : Main.conns) { 
          if (!con.master) { 
           synchronized (con) { 
            con.slaveCmd = cmd; 
           } 
          } 
         } 
        } 
       } else { 
        if (slaveCmd != null) { 
         synchronized (this) { // Here needs to be an breakpoint 
          out.writeln(slaveCmd); 
          slaveCmd = null; 
         } 
        } 
       } 
      } 
      ClientSocket.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       ClientSocket.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

shloud發生什麼:

客戶端1連接到服務器類型的「奴隸」 並等待,直到一些命令得到sended它

客戶端2在「主」連接類型併發送一些命令 是那麼shloud得到sended到連接爲「奴」

所有客戶端會發生什麼: 客戶端1連接爲從 客戶端2連接的主機發送一個命令,但是奴隸不得到它(它不會在Telnet客戶端窗口顯示)

代碼工作正常只有在調試器,並與斷點正確的地方

PS是的,我試着用搜索引擎卻一無所獲

回答

1

TL; DR ...

你不能肯定的是,正在運行的線程獲取布爾標誌主控的更新值,聲明揮發性代替...

public volatile boolean master; 
+1

非常感謝您的工作! – lukas2005

+0

歡迎您! –

相關問題