服务器没有使用android中的BufferedReader.readLine()进行读取

时间:2014-11-06 11:54:27

标签: java android sockets

我正在为java编写一个简单的客户端服务器应用程序,并在模拟器中运行客户端和服务器应用程序。我的服务器正在 10.0.2.15:4444 收听,我将 localhost:8080 (开发机器的127.0.0.1:8080)重定向到 10.0.2.15:4444 < / strong>。客户端正在与服务器建立连接并向服务器发送消息&#34; Hello Server!\ r \ n&#34; 但我的服务器未读取此消息。

**服务器端代码**

public void run()
    {
        try
        {
            server_socket = new ServerSocket(port_number);

            handle.post(new Runnable()
            {
                public void run()
                {
                    text.append("ServerSocket object has been created successfuly...\n");
                    text.append("Waiting for clients...\n");
                }
            });


            while(true)
            {
                Socket client = server_socket.accept();

                handle.post(new Runnable()
                {
                    public void run()
                    {
                        text.append("Client has come...\n");
                    }
                });



                try
                {
                    BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream()) );
                    String line ;


                    text.append("writing data....\n");

                        text.append("yes m ready...\n");

                        line = input.readLine();

                        text.append("yo i have come..\n");

                            if(line == null)
                            {
                                text.append("Nothing has been receieved...\n");
                            }
                            else
                            {
                                text.append(line);

                            while((line = input.readLine()) != null)
                            {
                                text.append(line);
                            }
                        }



                }
                catch(Exception e)
                {
                    text.append("problem in receving data...\n");
                }


            }


        }
        catch(Exception e)
        {
            text.setText("Error in running thread.\n");
        }
    }

**客户端代码**

public void run()
    {
        boolean connect;
        try
        {
            Socket server = new Socket(ip_address,port_number);
            text.append("connection has been made...\n");
            connect = true;

                try
                {
                    text.append("Preparing to send data...\n");
                BufferedWriter output = new BufferedWriter(new OutputStreamWriter(server.getOutputStream()));
                    output.write("Hello server.!\r\n");
                    output.flush();
                    text.append("data has been sent...\n");
                }
                catch(Exception e)
                {
                    text.append("Unable to send data...\n");
                }



        }
        catch(Exception e)
        {
            text.append("Unable to connect with server...\n");
        }
    }

我的客户端应用程序正在发送数据并显示数据已发送... (我写了这行进行调试),我的服务器没有读取数据并显示写入数据.. 。。 我试过这些线程

buffered reader not receiving data from socket
Sockets, BufferedReader.readline() - why the stream is not ready?
BufferedReader.readline() return nothing in result in android
BufferedReader.readline() return nothing in result in android

但没有什么对我有用。当我使用 BufferedReader.ready()时,它也阻止在 BurreferedReader.ready()(input.ready())。任何人都能告诉我我的代码有什么问题吗?

0 个答案:

没有答案