2012-12-16 125 views
6

我正在創建一個服務器,客戶端連接玩一個猜謎遊戲,他們也得到這樣做的要點。Java服務器猜測遊戲

我唯一的問題是,當我的客戶端正確猜測數字時,它跳轉到服務器並說'服務器爲空'。我想讓猜謎遊戲繼續下去,直到客戶輸入'再見' - 他/她得分。

這裏是我的代碼,你可以指出我出錯的地方,並告訴我如何實現我想要的。我認爲問題出在協議上,我可能只需要把時間放在正確的位置,所以這是第一次。謝謝你們! 我想補充,變量名字古怪的我知道這一點,以前是敲門笑話服務器

協議

import java.util.*; 

     public class KKProtocol { 
    int guess = 0, number = new Random().nextInt(100) + 1; 
    int score = 0; 
    Scanner scan = new Scanner(System.in); 


    public String processInput(String theInput) { 
     String theOutput = null; 

     System.out.println("Please guess the number between 1 and 100."); 

     while (guess != number) { 
      try { 
      if ((guess = Integer.parseInt(scan.nextLine())) != number) { 
       System.out.println(guess < number ? "Higher..." : "Lower..."); 
      } 
      else { 
       System.out.println("Correct!"); 
       score = 1; 
      } 
      } 
      catch (NumberFormatException e) { 
      System.out.println("Please enter a valid number! If you want to Quit just say'Goodbye'"); 
      } 

     } 



     return theOutput; 
    }} 

服務器

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

public class KKServer { 
    public static void main(String[] args) throws IOException { 
     ServerSocket serverSocket = null; 
     boolean listening = true; 
     try { 
      serverSocket = new ServerSocket(4040); 
     } catch (IOException e) { 
      System.err.println("Could not listen on port: 4040."); 
      System.exit(-1); 
     } 
     System.err.println("Started KK server listening on port 4040"); 
     while (listening) 
      new KKThread(serverSocket.accept()).start(); 
      System.out.println("Accepted connection from client"); 
      serverSocket.close(); 
     } 
} 

主題

import java.net.*; 
import java.io.*; 
import java.util.Scanner; 

public class KKThread extends Thread { 
    private Socket mySocket = null; 

    public KKThread(Socket inSocket) {   //super("KKThread"); 
     mySocket = inSocket; 
    } 
    public void run() { 

     try { 
      PrintWriter out = new PrintWriter(mySocket.getOutputStream(), true); 
      Scanner in = new Scanner(mySocket.getInputStream()); 

      String inputLine, outputLine; 
      KKProtocol kkp = new KKProtocol(); 

      outputLine = kkp.processInput(null);  // first time only 
      out.println(outputLine);  // (Should be "Knock Knock") 

      while (true) { 
       inputLine = in.nextLine();     // read in client input 
       outputLine = kkp.processInput(inputLine); // get reply from protocol 
       out.println(outputLine);     // send it out to socket 
       if (outputLine.equals("Bye")) 
        break; 
      } 
      out.close(); 
      in.close(); 
      mySocket.close(); 

     } catch (Exception e) { 
      System.err.println("Connection reset"); //e.printStackTrace(); 
     } 
    } 
} 

客戶端

import java.io.*; 
import java.net.*; 
import java.util.Scanner; 

public class KKClient { 
    public static void main(String[] args) throws IOException { 

     Socket kkSocket = null; 
     PrintWriter out = null; 
     Scanner in = null; 

     try { 
      kkSocket = new Socket("127.0.0.1", 4040); 
      out = new PrintWriter(kkSocket.getOutputStream(), true); 
      in = new Scanner(new InputStreamReader(kkSocket.getInputStream())); 
     } catch (UnknownHostException e) { 
      System.err.println("Don't know about host."); 
      System.exit(1); 
     } catch (IOException e) { 
      System.err.println("Couldn't get I/O for the connection"); 
      System.exit(1); 
     } 
     Scanner stdIn = new Scanner(System.in); 
     String fromServer = in.nextLine(); 
     while (true) { 
      System.out.println("Server: " + fromServer); 
      if (fromServer.equals("Bye.")) 
       break; 
      out.println(stdIn.nextLine()); 
      fromServer = in.nextLine(); 
     } 
     out.close(); 
     in.close(); 
     stdIn.close(); 
     kkSocket.close(); 
    } 
} 
' 

爲什麼它跳到服務器並說'服務器爲空'?我如何繼續猜測,直到客戶輸入'再見'?

+1

你的問題是什麼?你怎麼了? – ajacian81

回答

2

您目前沒有指定在KKProtocol.processInput()score服務器的響應所以null而是返回所產生的郵件中,你看到:

Server: null 

你可以使用:

theOutput = Integer.toString(score); 

而且你的score固定在1,所以你可能希望設計一個評分系統,也許根據使用的猜測數量。

1

在你的processInput()方法中,你沒有返回任何值,但總是null。 看起來,空值在輸出中被轉換爲字符串「null」併發送到客戶端。