0

I am required to design an application in Android which requires the phone to connect to a server by opening a socket. I am able to achieve this when I am just connected to the particular wifi network (ie the Wifi network which hosts the server ) but in a situation when I am connected to the wifi network and the Mobile data network I get a socket exception thrown as android tries to connect the socket over the mobile network

I have already been able to connect the device when its just connected to the wifi of the device that needs the socket connection to be established

  static class StartTCPconnection extends AsyncTask<Void, Void, Void> {
    final WeakReference<RemoteActivity> activity;

    StartTCPconnection(WeakReference<RemoteActivity> activity) {
        this.activity = activity;
    }


    @Override
    protected Void doInBackground(Void... voids) {
        try {
            socket = new Socket("192.168.4.1", 900);
            Log.d(TAG, "is socket connected ? ...." + socket.isConnected());
            printWriter = new PrintWriter(socket.getOutputStream(), true);
            Log.i(TAG, "Checking if socket is really connected " + (socket.getLocalSocketAddress()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        if (socket != null) {
            if (socket.isConnected() && isWifi) {
                Log.d(TAG, "onPostExecute: " + socket.isConnected());
                Toast.makeText(activity.get(), "Connection established", Toast.LENGTH_SHORT).show();
                Log.e(TAG, "onPostExecute: " + activity.get().getSharedPreferences(Constants.REMOTE_SWITCH_SHARED_PREFERENCE, Context.MODE_PRIVATE).getInt(Constants.REMOTE_SWITCH_KEY, 99));
                if (activity.get().getSharedPreferences(Constants.REMOTE_SWITCH_SHARED_PREFERENCE, Context.MODE_PRIVATE).getInt(Constants.REMOTE_SWITCH_KEY, 1) == 1) {
                    activity.get().joyStickFragment.checkSocketInstance(socket);
                    activity.get().joyStickFragment.changeUIForConnect();
                } else if (activity.get().getSharedPreferences(Constants.REMOTE_SWITCH_SHARED_PREFERENCE, Context.MODE_PRIVATE).getInt(Constants.REMOTE_SWITCH_KEY, 1) == 2) {
                    Log.e(TAG, "onPostExecute:Check  " + socket.isConnected());
                    activity.get().buttonRemoteFragment.checkSocketInstance(socket);
                    activity.get().buttonRemoteFragment.changeUIForConnect();
                }
                activity.get().connectionIndicatorImage.setImageResource(R.drawable.avishkaar_logo_on);
                activity.get().wifiIndicator.setImageResource(R.drawable.wifi_connected_icon);
            }

        } else {
            //   Toast.makeText(activity.get(), "Wrong Wifi Network connected", Toast.LENGTH_SHORT).show();
        }

    }
}

The above mentioned code connects me to the socket if the only network available is the WiFi of the device and the mobile network is turned off

1 Answer 1

1

There is nothing special from the programming perspective between connecting to a server which is in the local network and a server which is not. The only requirement is that the server is actually reachable in the first place, i.e. not in a private unreachable network and not blocked by a firewall or similar. And of course that the public reachable address of the server is used as destination in the program.

        socket = new Socket("192.168.4.1", 900);

192.168.4.1 is an address in a private network. This means it is not accessible from the internet, which also means that it cannot be reached if your are connecting with mobile data or if you use wifi within a different network (like a public hotspot).

To make a connection from outside this private network possible the server must be reachable from outside this network, i.e. needs to have a public routable IP address. If the server is in some typical home network this can be achieved with port forwarding in the router. For larger setups such servers are located at data centers directly reachable from the internet or (as a special case of this) in the cloud.

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

3 Comments

I understand that ... It is an IOT device which works by forming its own AP. What I wanted to know is if there is a way to make the android device connect to it without switching off Mobile data which I have to do right now to connect to it.
@FaizanMir: again: you are using a private IP address as destination to access the system and this address is not accessible from the internet (because it is a private and not a public IP address). You need to have the system available on the internet in the first place and then use the IP address the system has on the internet - and not the one it has on the local network. As long as the only connectivity the device offers is through its own AP it will not be accessible from the internet. So you have to find a way to integrate it into your existing network and then add port forwarding.
Ty I understand it

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.