0

I am trying to out put a random number from my server. I have the random number set up and converted into a string for the buffer reader but i am still getting an error, can anyone see where I am going wrong?

If anyone is interested I have worked on the code and it is now working as it should

Updated Working Server code

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


    public class server extends Thread
    {   
        private ServerSocket serverSocket;


        public server(int port) throws IOException
        {
            serverSocket = new ServerSocket(port);
            serverSocket.setSoTimeout(10000);
        }


        public void run()
        {
            System.out.println("Starting game...");


            while(true)
            {

                System.out.println("Client connection established! Game started");
                try
                {


                    Socket server = serverSocket.accept();

                    Random rand = new Random();
                    int randInt = rand.nextInt(12);             

                    DataOutputStream out = new DataOutputStream(server.getOutputStream());
                    out.writeUTF("Turning on button " + randInt);
                    DataInputStream in = new DataInputStream(server.getInputStream());
                    System.out.println(in.readUTF());
                    out.writeUTF("Acknowledged - Button 1 pressed");
                }// End try


                catch(SocketTimeoutException s)
                {   
                    System.out.println("Socket timed out!");

                    break;
                }// End catch

                catch(IOException e)
                {
                    e.printStackTrace();

                    break;
                }// End catch
            }// End while()
        }// End run()

        /*The start of the main loop */
        public static void main(String [] args)
        {

            int port = 4444;
            try
            {
                Thread t = new server(port);

                t.start();

            }// End try

            catch(IOException e)
            {   

                e.printStackTrace();
            }// End catch
        }// End main()
    }/

Updated Working Client Code

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

    public class client
    {
        public static void main(String [] args)
        {
            String serverName = "localhost";

            int port = 4444;
            try
            {
                Socket client = new Socket(serverName, port);


                Random rand = new Random();
                int randInt = rand.nextInt(12);
                OutputStream outToServer = client.getOutputStream();
                DataOutputStream out = new DataOutputStream(outToServer);
                out.writeUTF("Button " + randInt + " pressed");
                InputStream inFromServer = client.getInputStream();
                DataInputStream in = new DataInputStream(inFromServer);

            }// End client

            catch(IOException e)
            {
                e.printStackTrace();
            }// End catch
        }// End main
    }
3
  • it says that the string random is an incompatible type, I thought string was the type for buffer reader Commented Mar 16, 2015 at 11:59
  • Could you put the complete stack trace and also the client program used. Commented Mar 16, 2015 at 12:00
  • sure just put the client there Commented Mar 16, 2015 at 12:03

3 Answers 3

2

new InputStreamReader(random) Here is the error. There is no such constructor of InputStreamReader as InputStreamReader(String). I am not really sure what you are trying to acheive here. Otherwise you can use something like

PrintWriter outToClient = new PrintWriter(connected.getOutputStream(),true);
out.print(random);
Sign up to request clarification or add additional context in comments.

Comments

1

Your problem is in the server class here:

 BufferedReader inFromUser = new BufferedReader(new InputStreamReader(random));    

InputStreamReader takes a InputSream as an argument, not a String.


BTW your main method takes an int[] array, not a String[]. It won't run like this!

Comments

0

Readers RETURN strings, not take them in, so in that respect they are related. They do not work the way you have here; I suggest you read the documentation so you know how to use readers in the future.

In any case, readers are used to read text in from some sort of stream. You already have your text, so you don't need the reader.

At the end of your code in the server, add outToClient.println(random); and remove your BufferedReader entirely.

while(true) 
{

    //listens for connection
    Socket connected = Server.accept();

    //prints clients adddress and port
    System.out.println( " THE CLIENT"+" "+ connected.getInetAddress() +":"+connected.getPort()+" IS CONNECTED ");

    Random rannum = new Random();
    int num = rannum.nextInt(12);

    String random = Integer.toString(num);

    System.out.println("Turning on Button " + num);

    PrintWriter outToClient = new PrintWriter(connected.getOutputStream(),true);
    outToClient.println(random);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.