0

I am writing a simple client server application for android in java and running client and server application in emulators. My server is Listening at 10.0.2.15:4444 and i redirected localhost:8080(127.0.0.1:8080 of development machine) to 10.0.2.15:4444 . Client is getting connected with server and sending server a message "Hello Server !\r\n" but my server is not reading this message .

**Server Side Code **

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

**Client Side Code **

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

My client application is sending data and showing Data has been sent...(i wrote this line for doing debugging) and my server is not reading data and showing writing data... . I have tried these threads

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

but nothing is working for me . When i used BufferedReader.ready() then also it was blocking at BurreferedReader.ready() (input.ready()) . Can any one tell me what is wrong with my code ?

Community
  • 1
  • 1
  • For a test add output.close(); and server.close(); to your client to see if that makes any difference. – greenapps Nov 06 '14 at 12:10
  • @greenapps added , still not reading data :( – Saurabh Jain Nov 06 '14 at 14:12
  • You say that you are seeing: `text.append("writing data....\n");` (which is a strange text but ala). But are you not seeing `text.append("yes m ready...\n");` ? What is the last code that gets executed? Are you catching `text.append("Unable to send data...\n");` ? Better change that to `text.append("Exception: " + e.getMessage());`. – greenapps Nov 06 '14 at 14:53
  • @greenapps yes i did not consider this , last statement it is printing is "writting data...\n" .... and m not getting why it is happening ?? – Saurabh Jain Nov 06 '14 at 16:09
  • Strange behaviour , when i print client message above these lines , it is getting there , might be problem with TextView which i m using ... – Saurabh Jain Nov 06 '14 at 16:19
  • Sorry, don't understand you. But is your problem solved now? – greenapps Nov 07 '14 at 10:03
  • @greenapps not completely . Thanx for reply. – Saurabh Jain Nov 07 '14 at 18:31

0 Answers0