2012-12-16 14 views
6

Tworzę serwer, na którym klienci łączą się, aby zagrać w grę zgadującą, a także zdobyć punkty za zrobienie tego.Gra Zgadywanie Serwera Java

W tej chwili moim jedynym problemem jest to, że ilekroć mój klient odgadnie numer, przeskakuje on do serwera i mówi "server null". Chcę, aby gra polegająca na odgadywaniu była kontynuowana do momentu, aż klient wejdzie w "pożegnanie" - na której jest podany jego wynik.

Oto mój kod, możesz wskazać, gdzie się nie udałem i doradzić, w jaki sposób osiągnąć to, co chcę. Myślę, że problem leży w protokole. Prawdopodobnie muszę położyć chwilę w odpowiednim miejscu, więc to jest pierwsze. Dzięki ludziom! Wystarczy dodać, zmienne są nazwane dziwnie jestem świadomy tego, że był wcześniej domina domina serwer żart

Protokół

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; 
    }} 

Server

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(); 
     } 
} 

wątku

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(); 
     } 
    } 
} 

Klient

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(); 
    } 
} 
' 

Dlaczego przeskakuje do serwera i wyświetla komunikat "server null"? Jak kontynuować zgadywanie, dopóki klient nie wejdzie "na pożegnanie"?

+1

Jakie jest twoje pytanie? Co ci dolega? – ajacian81

Odpowiedz

2

Obecnie nie przypisując odpowiedź serwera dla score w KKProtocol.processInput() więc null jest zwracana zamiast powstałego w komunikacie, który można zobaczyć:

Server: null 

możesz użyć:

theOutput = Integer.toString(score); 

także Twój score jest ustawiony na 1, więc możesz chcieć opracować system punktacji, być może oparty na liczbie użytych domysłów.

1

W tobie processInput() metoda, nie zwracasz żadnej wartości, ale zawsze null. Wygląda na to, że wartość pusta jest przekształcana w łańcuch znaków "null" na wyjściu i wysyłana do klienta.