Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java Server Guessing Game

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Server Guessing Game

    I'm a self confessed noob so don't be too harsh if there's a idiotic mistake! To put it simply I'm creating a server where clients connect to play a guessing game they are also to get points from doing this. My only problem at the moment is that whenever my client guesses the number correctly it jumps to the server and says 'server null' I want the guessing game to continue until the client inputs 'goodbye' - on which his/her score is given. Here is my code could you point out where I've gone wrong and advise me on how I would achieve what I want. I think the problem is in protocol I probably just need to put the while in the correct place, so that's up first. Thanks folks!
    Just to add the variables are named oddly I'm aware of this it was previously a knock knock joke server

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java Server Guessing Game

    says 'server null'
    What is the "it" that says that? Is there an error or is there a print statement in the code that prints that message?
    Is this the statement:
    System.out.println("Server: " + fromServer);
    Was the message that was printed: Server: null?

    That would mean that fromServer has a null value.


    Can you post the full contents of the console from when you execute the program.

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. JAVA Guessing Game Problem
    By adnan.alvee in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2012, 12:02 AM
  2. Guessing Game
    By scottey93 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 7th, 2011, 02:50 PM
  3. Java Newbie..need help in guessing game program
    By confupavan in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 16th, 2011, 09:22 AM
  4. JAVA HIGH LOW NUMBER GUESSING GAME
    By Laxman2809 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 12th, 2011, 10:50 AM
  5. im in a hurry!!Help with a programm..java game of guessing a word
    By mr_doctor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 08:17 AM

Tags for this Thread