0

I have a problem about messaging between two android devices via TCP/IP. The problem is I can't send any messages to a 2nd Android device. When I enter the local IP of the 1st device I can send messages to myself. Here are my Server and Client classes :

Server class:

public class ServerThread implements Runnable {

        public void run() {
            try {
                if (SERVERIP != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            //serverStatus.setText("Listening on IP: " + SERVERIP);
                        }
                    });
                    serverSocket = new ServerSocket(8080);
                    while (true) {
                        // listen for incoming clients
                        Socket client = serverSocket.accept();

                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                //serverStatus.setText("Connected.");
                            }
                        });

                        try {
                         BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));

                         String line = null;
                          while ((line = in.readLine()) != null) {

                                final String a=line.toString();
                                Log.d("ServerActivity", line);
                                handler.post(new Runnable() {
                                  @Override
                                  public void run() {


                                        thelist.add(a);
                                        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.chat_view, R.id.chat_view_label, thelist);
                                        lvMessage.setAdapter(arrayAdapter);
                                        //onStop();                
                                        try {
                                            serverSocket.close();
                                        } catch (IOException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }


                                    }

                                });

                            }

                            break;
                        } catch (Exception e) {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    //serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones.");
                                }
                            });
                            e.printStackTrace();
                        }
                    }
                } else {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            //serverStatus.setText("Couldn't detect internet connection.");
                        }
                    });
                }
            } catch (Exception e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        //serverStatus.setText("Error");
                    }
                });
                e.printStackTrace();
            }



        }
    }

Client class :

public class ClientThread implements Runnable {

        public void run() {

            try {


                InetAddress serverAddr = InetAddress.getByName("141.196.41.145");


                Log.d("ClientActivity", "C: Connecting...");
                Socket socket = new Socket(serverAddr,8080);

                //Socket socket = new Socket("141.196.41.145",8080);


               connected=true;

                while (connected) {

                    try {
                      Log.d("ClientActivity", "C: Sending command.");
                       PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                            // where you issue the commands
                            out.println(" MT 02 : " + thevariable);
                            Log.d("ClientActivity", "C: Sent.");
                            break;

                    } catch (Exception e) {
                        Log.e("ClientActivity", "S: Error", e);
                    }


                    }
                socket.close();
                Log.d("ClientActivity", "C: Closed.");
            } catch (Exception e) {
                Log.e("ClientActivity", "C: Error", e);
                connected=false;
            }
        }
    }

There "141.196.41.145" is the local IP of my 2nd device. If I change this IP to my 1st device's local IP I can send messages to myself.

Thanks for your interest and help

2
  • Are you checking with 2 emulators? Commented Nov 25, 2013 at 8:38
  • No. I'm using two android devices Commented Nov 25, 2013 at 8:52

1 Answer 1

1

Where did you use the socket.connect(); here ?

            InetAddress serverAddr = InetAddress.getByName("141.196.41.145");


            Log.d("ClientActivity", "C: Connecting...");
            Socket socket = new Socket(serverAddr,8080);

Creating a socket doesn't make conncection

Sign up to request clarification or add additional context in comments.

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.