Hayes121
Hayes121

Reputation: 283

Java Socket BufferReader

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
    }

Upvotes: 0

Views: 70

Answers (3)

Russell Uhl
Russell Uhl

Reputation: 4531

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

Upvotes: 0

Mandeep Rajpal
Mandeep Rajpal

Reputation: 1777

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

Upvotes: 2

Mordechai
Mordechai

Reputation: 16234

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!

Upvotes: 1

Related Questions