简体   繁体   English

Java Socket BufferReader

[英]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
    }

new InputStreamReader(random) Here is the error. new InputStreamReader(random)这是错误。 There is no such constructor of InputStreamReader as InputStreamReader(String) . 没有InputStreamReader(String)这样的InputStreamReader构造函数。 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);

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 . InputStreamReader采用InputSream作为参数,而不是String


BTW your main method takes an int[] array, not a String[] . 顺便说一句,您的main方法需要一个int[]数组,而不是String[] It won't run like this! 它不会像这样运行!

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); 在服务器中代码的末尾,添加outToClient.println(random); and remove your BufferedReader entirely. 并完全删除您的BufferedReader

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM