Results 1 to 2 of 2
  1. #1
    SlickJava is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Guessing Game Client/Server connection problem

    Hi, I'm trying to make a program which has a server which a client can connect to. The server waits for connections, and when it recieves one, it calls a Worker thread to handle the client. Instead of the output coming out like this:

    Java Code:
    SERVER:
    
    Waiting for connection....
    Connection 1 recieved from 127.0.0.1:53
    Waiting for connection....
    
    CLIENT:
    
    Attempting connection....
    Connected to server
    Got Streams
    You are now connected
    Please type in a guess.
    5
    You guessed wrong, please try again.
    2
    You won!
    It comes out like this:

    Java Code:
    SERVER:
    
    Waiting for connection....
    Connection 1 recieved from 127.0.0.1:53
    Waiting for connection....
    
    CLIENT:
    
    Attemepting connection...
    Connected to server 
    Got streams
    You are now connected
    2
    5
    6
    Every time I input a number in the client side of the program, it just comes up with nothing. The code is below. Can anyone help?

    SERVER + Worker:
    Java Code:
    package com.jason.guessSocket1;
    
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Random;
    
    public class Server
    {
    
        private ObjectInputStream input;
    
        private ObjectOutputStream output;
    
        private Socket connection;
    
        private ServerSocket server;
    
        private int counter = 0;
    
        public void startRunning()
        {
            try
            {
                server = new ServerSocket(53);
                while (true)
                {
                    waitForConnection();
                }
            }
            catch (Exception e)
            {
    
            }
        }
    
        public void waitForConnection() throws IOException
        {
            System.out.println("Waiting for connection...\n");
            connection = server.accept();
            new Thread(new Worker()).start();
            counter++;
            System.out.println("Connection " + counter + " recieved from: "
                            + connection.getInetAddress().getHostName());
        }
    
        public class Worker implements Runnable
        {
    
            private int guess;
    
            private final int target;
    
            private final Random ran;
    
            public Worker()
            {
                ran = new Random();
                target = ran.nextInt(10) + 1;
    
            }
    
            public void proccessConnection()
            {
                try
                {
                    guess = input.readInt();
                    System.out.println("Client guessed - " + guess);
    
                }
                catch (Exception e)
                {
    
                }
            }
    
            public void setUpStreams() throws IOException
            {
                output = new ObjectOutputStream(connection.getOutputStream());
                output.flush();
                input = new ObjectInputStream(connection.getInputStream());
            }
    
            public void askForGuess() throws IOException
            {
                sendMessage("Please type in a guess \n");
                System.out.println("Client guessed: " + guess);
    
            }
    
            public void checkGuess() throws IOException
            {
                while (guess != target)
                {
                    sendMessage("You guessed wrong, please try again \n");
                    askForGuess();
                }
    
                sendMessage("You won!\n");
            }
    
            public void sendMessage(String message) throws IOException
            {
                output.writeObject(message);
                output.flush();
            }
    
            @Override
            public void run()
            {
                try
                {
                    setUpStreams();
                    proccessConnection();
                    askForGuess();
                    checkGuess();
    
                }
    
                catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
        public static void main(String args[])
        {
    
            Server s = new Server();
            s.startRunning();
    
        }
    }
    CLIENT:

    Java Code:
    package com.jason.guessSocket1;
    
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.util.Scanner;
    
    public class Client
    {
        private int guess;
    
        Scanner scan;
    
        private Socket connection;
    
        private ObjectOutputStream output;
    
        private ObjectInputStream input;
    
        public Client()
        {
            scan = new Scanner(System.in);
        }
    
        public void startRunning()
        {
            try
            {
                connectToServer();
                setUpStreams();
                proccessConnection();
            }
            catch (Exception e)
            {
    
            }
        }
    
        public void connectToServer()
        {
            try
            {
                System.out.println("Attmepting connection...");
                connection = new Socket(InetAddress.getByName("localhost"), 53);
                System.out.println("Connected to server ");
            }
            catch (Exception e)
            {
                System.out.println("Cannot find server ");
            }
        }
    
        public void setUpStreams() throws IOException
        {
            output = new ObjectOutputStream(connection.getOutputStream());
            output.flush();
            input = new ObjectInputStream(connection.getInputStream());
            System.out.println("Got streams");
        }
    
        public void proccessConnection()
        {
            String message = "You are now connected";
            System.out.println(message);
            try
            {
                message = (String) input.readObject();
                System.out.println(message);
    
            }
            catch (Exception e)
            {
    
            }
        }
    
        public void sendGuess(int guess1) throws IOException
        {
    
            output.writeInt(guess1);
            output.flush();
        }
    
        public static void main(String args[])
        {
            Client c = new Client();
            c.startRunning();
    
        }
    }
    Thanks!
    SlickJava

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default Re: Guessing Game Client/Server connection problem

    I didn't copy/paste/compile/test the code, but I did notice a couple of things. First of all, what happens if there's an exception here:

    Java Code:
        public void proccessConnection()
        {
            String message = "You are now connected";
            System.out.println(message);
            try
            {
                message = (String) input.readObject();
                System.out.println(message);
     
            }
            catch (Exception e)
            {
     
            }
        }
    Second, there's no loop in the run() method of the worker, so at most it'd only ask for one guess.

    Third, the best way to bughunt (at least to me) is to put in a ton of System.out.println (or any other output that fits your fancy) and see where the code breaks down. Once you've found that out, focus on that one bit.

Similar Threads

  1. Replies: 16
    Last Post: 08-31-2012, 01:06 AM
  2. Replies: 3
    Last Post: 06-03-2012, 09:48 AM
  3. Replies: 1
    Last Post: 06-03-2012, 03:07 AM
  4. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  5. guessing game problem
    By MrM in forum New To Java
    Replies: 3
    Last Post: 02-03-2011, 11:03 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •