簡體   English   中英

我想在此程序中設置一個計時器,以便在經過一定時間后它將退出循環

[英]I want to set a timer in this program so that it will break out of the loop if a certain period of time has passed

代碼的重點是一個連接客戶端的游戲,然后讓他們猜測一個按鈕……然后讀取輸入並將其與生成的內容進行比較。 我希望為服務器關閉之前客戶端必須輸入的時間設置一個時間限制。

   while(true)
        {
           try
            {

            //accept a client connection
            Socket GameServer = SSocket.accept();



            //let the server know the game is starting 
            System.out.println("Starting game...");

            //generate random number
            randnum = new Random();
            //make it be a button from 0-12
            thebutton = randnum.nextInt(11);
            //acknowledge which button was generated
            System.out.println("Button " + thebutton + " turned on");

            //writes server message to client
            BufferedReader in = new BufferedReader(new InputStreamReader(GameServer.getInputStream()));
            //make the button guessed = to whatever the client has entered
            input = in.readLine();
            //let the server know the clients response
            System.out.println("Button press acknowledged \nPlayer pressed button " + input);
            //let the client know the server acknowledged the response. 
            PrintWriter out = new PrintWriter(GameServer.getOutputStream());

            //convert user input from string to int
            buttonguess = Integer.parseInt(input);
            //compare input against number generated
            if (buttonguess == thebutton)
            {
                //if it's right print that the button is correct
                System.out.println("Player pressed the correct button");
                //add one to user score
                points += 1;
            }

            else
            {
                //if it's wrong then game over let the user know this
                System.out.println("Incorrect button pressed\nGame over!");
                //let the user know what score they have got
                System.out.println("Score = " + points);
                break;
            }

        //DataOutputStream confirmation = new DataOutputStream(SSocket.getOutputStream());

        //confirmation.writeBytes("I got your message"); //Responds to client using the DataOutputStream
        }// End try

        //if the time runs out 


        //if the number entered is incorrect format (not a number)
        catch(NumberFormatException n)
        {
            //let the user know there was a problem with their input
            System.out.println("Game over you didn't enter a proper input better luck next time \n");
            //print their score
            System.out.println("Your Score is = " + points);
            break;
        }

                catch(SocketTimeoutException a)
        {   

            System.out.println("Time up \n Game over!");
            System.out.println("Your Score is = " + points);
            //exit program
            break;
        }// End catch
        //if any kind of error occurs (usually connection error then do the following)
        catch(IOException e)
        {
            //print that error
            e.printStackTrace();

            // exit the program
            break;
        }// End catch
    }// End while()

Reader.read方法(包括readLine正在阻塞,因此您無法將它們與超時結合使用。

相反,您可以使用Reader.ready首先測試是否可以無阻礙地讀取流(即,是否有任何輸入要讀取)。

因此,您可以按照以下方式進行操作:

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String input = null;
    long maxWait = 5000; // 5 seconds
    long endTime = System.currentTimeMillis() + maxWait;
    boolean timeout = false;
    while (!reader.ready()) {
        synchronized(reader) {
            reader.wait(250);
        }
        if (System.currentTimeMillis() >= endTime) {
            timeout = true;
            break;
        }
    }
    if (!timeout) {
        input = reader.readLine();
    } else {
        System.out.println("timeout without input");
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM