0
我想運行一個簡單的服務器客戶端程序,其中客戶端檢查每個5秒最佳服務器並連接總是最好的服務器。所以我寫了下面的代碼,但我得到了stackoverflow錯誤。兩線程共享靜態變量
public static void main(String[] args) throws IOException, InterruptedException {
RTT best = null;
BestServer bestserver = new BestServer(best);
Socket clientSocket = new Socket(bestserver.bestserver.ip, 6789);
System.out.println("I connect best server");
}
public class BestServer implements Runnable{
static RTT bestserver;
public BestServer(RTT best) throws InterruptedException{
bestserver = best;
findBest();
}
public static void findBest() throws InterruptedException{
Thread t = new Thread(new BestServer(bestserver));
t.start();
t.sleep(5000);
}
public void run(){
//..
}
'while(true)'huh? –
這是客戶端還是服務器?如果它是服務器,你可能想看看ServerSocket類。 http://docs.oracle.com/javase/7/docs/api/java/net/ServerSocket.html – Nathan
不,它是客戶端。客戶端連接rtt總是最好的服務器,並且他每5秒檢查一次bestserver – user1914367