Server Client按钮游戏循环

时间:2015-03-24 13:14:57

标签: java sockets

我一直在创建一个小游戏,涉及用户在生成的客户端输入数字但服务器。我让它在一个循环中工作,但我无法保持游戏的进行。

一旦用户输入他们的号码,服务器将在需要的地方给出分数,我尝试在下面的行周围添加一个for循环,但这只是打印使用用户的第一个输入。

如何更改代码,以便服务器为客户端发出新号码,而不只是使用用户的第一个号码?

试图循环播放此代码

                int n = light();
                String binString = lightBin(n);
                String check = Integer.toString(n);
                System.out.println(binString);

                outToClient.println("Turning on light " + n);

                fromclient = inFromClient.readLine();
                System.out.println("User pressed light " + fromclient);

                score = scoreCheck(check, fromclient);
                System.out.println("Score = " + score);

服务器代码

    package networking;

    import java.io.*;
    import java.net.*;
    import java.text.DecimalFormat;
    import java.util.Random;

    class TCPServer extends Thread
    {
        private static Object outToClient;


       public void run()
       {
           System.out.println ("The game is about to begin.");

       }

       public static void main(String argv[]) throws Exception
       {

            //declaring string variables
             String fromclient;
             int score= 0;
             //int outToClient;

             //new socket object, listening on port 5000
             ServerSocket Server = new ServerSocket (3030);
             Server.setSoTimeout(10000);

             //prints when started and no client connected
             System.out.println ("The game is about to begin.");

             //infinate loop
             while(true) 
             {

                //listens for connection
                Socket connected = Server.accept();



                //outToClient.println(random);
                PrintWriter outToClient = new PrintWriter(connected.getOutputStream(),true);
                BufferedReader inFromClient = new BufferedReader(new InputStreamReader (connected.getInputStream()));


                int n = light();
                String binString = lightBin(n);
                String check = Integer.toString(n);
                System.out.println(binString);

                outToClient.println("Turning on light " + n);

                fromclient = inFromClient.readLine();
                System.out.println("User pressed light " + fromclient);

                score = scoreCheck(check, fromclient);
                System.out.println("Score = " + score);

          }
        }


       public static int scoreCheck(String check, String fromclient)
       {
           int score = 0;

           if (check.equals(fromclient))
               score++;
           else
               score--;

           return score;
       }

       public static String lightBin (int n)
       {
           String binString = Integer.toBinaryString(n);
            while (binString.length() < 16) {    //pad with 16 0's
                binString = "0" + binString;
            } 
            return binString;

       }

       public static int light ()
       {
            Random rand = new Random();
            int n = rand.nextInt(12) + 1;
            String check = Integer.toString(n);
            System.out.println("Turning on light " + n);
            return n;
       }

    }

客户代码

    package networking;

    import java.io.*;
    import java.net.*;

    class TCPClient 
    {
     public static void main(String argv[]) throws Exception
     {

        String serverName = "localhost";
        int port = 3030;

        String FromServer;
        String ToServer;

        Socket client = new Socket(serverName, port);
        client.setSoTimeout(15000);

        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

        FromServer = inFromServer.readLine();
        System.out.println(FromServer);

        PrintWriter outToServer = new PrintWriter(client.getOutputStream(),true);
        ToServer = inFromUser.readLine();


        while(true)
        {
            outToServer.println (ToServer) ;
        }
      }
    }

1 个答案:

答案 0 :(得分:0)

您的客户端代码应该是

 while (true) { // but you must specify some condition to stop the while
            FromServer = inFromServer.readLine();
            System.out.println(FromServer);

            PrintWriter outToServer = new PrintWriter(client.getOutputStream(), true);
            ToServer = inFromUser.readLine();

            outToServer.println(ToServer);
            outToServer.flush();
        }

和您的服务器端代码

while (true) { // but you must specify some condition to stop the while
                outToClient.println("Turning on light " + n);
                outToClient.flush();
                fromclient = inFromClient.readLine();
                System.out.println("User pressed light " + fromclient);

                score = scoreCheck(check, fromclient);
                System.out.println("Score = " + score);
            }