2014-04-25 84 views
-1

我正在做猜谜游戏。所有工作正常,但我有一个问题。第一次,我要求一个数字它正确地说我是否更高或更低,但第二没有说什么都没有..Java猜测游戏服务器 - 客户端

这是我的服务器和客户端代码:

public class ServerNum 
    { 
    public static void main(String[] args) throws IOException 
    { 
     adivinarNum juego = new adivinarNum(); 
     ServerSocket socket = null; 
     Socket client = null; 
     String resultado; 
     boolean correcto = false; 
     int intentos; 

       try{ 
        socket = new ServerSocket(1234); 

       }catch(IOException ioe) 

       { 
        System.err.println(ioe); 
        return; 
       } 
        System.out.println("El servidor sigue funcionando..."); 
        client = socket.accept(); 
        System.out.println("El cliente se ha conectado"); 
        DataInputStream in = new DataInputStream(new BufferedInputStream(client.getInputStream())); 
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(client.getOutputStream())); 
     while(!correcto) 
      { 
       intentos = in.readInt(); 
       resultado = juego.adivinar(intentos); 
       correcto = juego.getCorrecto(); 
       out.writeUTF(resultado); 
       out.writeBoolean(correcto); 
       out.flush(); 
      if(correcto == false){ 
       client = socket.accept(); 
       intentos = in.readInt(); 
       resultado = juego.adivinar(intentos); 
       correcto = juego.getCorrecto(); 
       out.writeUTF(resultado); 
       out.writeBoolean(correcto); 
       out.flush(); 
       } 
      else{ 
       client.close(); 
       socket.close(); 
       } 

      } 
     } 
    } 


     public class ClientNum 
     { 
     public static void main(String[] args) throws IOException 
     { 
      System.out.println("This is Number Guessing Game. \nChoose any number between 1 to 1000 : "); 
      Scanner keyboard = new Scanner(System.in); 
      int attempt = 0; 
      try 
      { 
      attempt = keyboard.nextInt(); 
      if(attempt < 1 || attempt > 999) 
      { 
       System.out.println("Your number is too large/small, please make a guess between 1 to 1000"); 
       attempt = keyboard.nextInt(); 
       } 
      } 
     catch(NumberFormatException nfe) 
     { 
      System.out.println("Just choose numbers! Try again"); 
      attempt = keyboard.nextInt(); 
     } 

     try 
       { 
        Socket server = new Socket("localhost", 1234); 
        System.out.println("Connecting..."); 

        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(server.getOutputStream())); 
        DataInputStream in = new DataInputStream(new BufferedInputStream(server.getInputStream())); 

        out.writeInt(attempt); 
        out.flush(); 
        System.out.println("Our server is still running..."); 
        String result = in.readUTF(); 
        boolean correct = in.readBoolean(); 
        System.out.println(result); 


        while (!correct){ 
          attempt = keyboard.nextInt(); 
          out.writeInt(attempt); 
          out.flush(); 
          System.out.println("Our server is still running..."); 
          result = in.readUTF(); 
          System.out.println(result); 
          correct = in.readBoolean(); 
        } 

         server.close(); 
         System.out.println("Finish. Thank you"); 
         System.exit(0); 

       }catch(IOException ioe){ 
        System.err.println(ioe); 
       } 
      } 
     } 

我会感谢任何帮助!谢谢!

+1

您每次客户端猜测错误时都调用'socket.accept()',我不认为这是正确的。 –

回答

1

socket.accept用于接受到客户端的连接。这意味着它用于开始与客户端通信,因此,每个连接只需要调用一次。此外,您的客户在获得正确答案后立即删除连接,然后到达其方法main()的末尾,这意味着该程序在那里结束。

你应该设计你的程序,以便建立连接,然后在while循环内(通常称为“游戏循环”)播放游戏代码,直到用户不想再玩。当用户不想再玩时,允许循环结束,然后让它关闭与服务器的连接,以便应用程序可以正常关闭。

应该指出,accept方法有一个效率开销与它相关联,因此如果你不必经常调用它,程序将更有效地运行。

+0

非常感谢你丰富:) – Coluh