Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9164659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:44:08+00:00 2026-06-17T14:44:08+00:00

I have two android tablets that I want to send messages from the client

  • 0

I have two android tablets that I want to send messages from the client to the server with socket programming. I pasted the sample code to make the server and client apps and installed them on two android tablets. both connected to wifi

on the client app there is an edit text that i am supposed to type in an IP address in it then the button below that is called “connect phones”.

what IP address am i supposed to type in the edittext before clicking the “connect phones button”

here is the onclick listener for the “connect phones” button

    private OnClickListener connectListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        if (!connected) {
            serverIpAddress = serverIp.getText().toString();
            if (!serverIpAddress.equals("")) {
                Thread cThread = new Thread(new ClientThread());
                cThread.start();
            }
        }
    }
};

full code for both client and server apps shown below;

client code:

 package com.example.client;

 // import statements

 public class ClientActivity extends Activity {

private EditText serverIp;

private Button connectPhones;

private String serverIpAddress = "";

private boolean connected = false;

private Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.client);

    serverIp = (EditText) findViewById(R.id.server_ip);
    connectPhones = (Button) findViewById(R.id.connect_phones);
    connectPhones.setOnClickListener(connectListener);
}

private OnClickListener connectListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        if (!connected) {
            serverIpAddress = serverIp.getText().toString();
            if (!serverIpAddress.equals("")) {
                Thread cThread = new Thread(new ClientThread());
                cThread.start();
            }
        }
    }
};

public class ClientThread implements Runnable {

    public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
            Log.d("ClientActivity", "C: Connecting...");
            Socket socket = new Socket(serverAddr, 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("Hey Server!");
                        Log.d("ClientActivity", "C: Sent.");
                } 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;
        }
    }
 }
 }

server code;

       package com.example.server;

  // import statements

  public class ServerActivity extends Activity {

private TextView serverStatus;

// default ip
// public static String SERVERIP ="10.0.2.15";

// designate a port
public static final int SERVERPORT = 8080;

private Handler handler = new Handler();

private ServerSocket serverSocket;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.server);
    serverStatus = (TextView) findViewById(R.id.server_status);

    SERVERIP = getLocalIpAddress();

    Thread fst = new Thread(new ServerThread());
    fst.start();
}

    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(SERVERPORT);
                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) {
                            Log.d("ServerActivity", line);
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    // do whatever you want to the front end
                                    // this is where you can be creative
                                }
                            });
                        }
                        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();
        }
    }
}

// gets the ip address of your phone's network
private String getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); }
            }
        }
    } catch (SocketException ex) {
        Log.e("ServerActivity", ex.toString());
    }
    return null;
}

@Override
protected void onStop() {
    super.onStop();
    try {
         // make sure you close the socket upon exiting
         serverSocket.close();
     } catch (IOException e) {
         e.printStackTrace();
     }
}

 }

logcat errors;

01-22 18:37:40.100: E/ClientActivity(19568): C: Error
01-22 18:37:40.100: E/ClientActivity(19568): java.net.ConnectException: failed to connect to /10.0.2.15 (port 8080): connect failed: EHOSTUNREACH (No route to host)
01-22 18:37:40.100: E/ClientActivity(19568):    at libcore.io.IoBridge.connect(IoBridge.java:114)
01-22 18:37:40.100: E/ClientActivity(19568):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-22 18:37:40.100: E/ClientActivity(19568):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
01-22 18:37:40.100: E/ClientActivity(19568):    at java.net.Socket.startupSocket(Socket.java:566)
01-22 18:37:40.100: E/ClientActivity(19568):    at java.net.Socket.<init>(Socket.java:225)
01-22 18:37:40.100: E/ClientActivity(19568):    at com.example.client.ClientActivity$ClientThread.run(ClientActivity.java:60)
01-22 18:37:40.100: E/ClientActivity(19568):    at java.lang.Thread.run(Thread.java:856)
01-22 18:37:40.100: E/ClientActivity(19568): Caused by: libcore.io.ErrnoException: connect failed: EHOSTUNREACH (No route to host)
01-22 18:37:40.100: E/ClientActivity(19568):    at libcore.io.Posix.connect(Native Method)
01-22 18:37:40.100: E/ClientActivity(19568):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
01-22 18:37:40.100: E/ClientActivity(19568):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
01-22 18:37:40.100: E/ClientActivity(19568):    at libcore.io.IoBridge.connect(IoBridge.java:112)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:44:09+00:00Added an answer on June 17, 2026 at 2:44 pm

    1.connect two device with a network (WIFI)
    2.you can get ip address of each device via programmatically in android
    3.and make a textview which shows the two ip address
    4.type it in both connect it

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Android devices. One is acting as a server and the other
I have two android applications that I have developed. I need to compare the
I have two links,for eg: say facebook.com and m.facebook.com.If it's android mobile, i want
I will have two android tablets working in the same retail location, both connected
Background I have an Android project that has a database with two tables: tbl_question
I have two tables: 1) One is the location table that is kept from
I have a .db file that contains the contacts list from an android phone.
android noob... I have two tables, with a one to many relationship between country_tbl
I have two Android applications with a similar functionality but different drawables and layouts.
I have two android applications. The apks are Hello.apk and Fonts.apk I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.