0

I'm developing one socket app.

Background :

  • There is two android device.
  • Both device is on different network (i.e. one is on mobilenetwork and other is on different mobile network or on wifi).
  • App is working good when on same wifi network.
  • App is not working on different network.
  • I've read about port forwarding but this all ended up with router port forwarding, so this has nothing to do with my app, as my app is like to work on different network.

What I've tried:

I got server ip address using following method and it gives me ip address:

    public String getIpAddress() {
    String ip = "";
    try {
        Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
                .getNetworkInterfaces();
        while (enumNetworkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = enumNetworkInterfaces
                    .nextElement();
            Enumeration<InetAddress> enumInetAddress = networkInterface
                    .getInetAddresses();
            while (enumInetAddress.hasMoreElements()) {
                InetAddress inetAddress = enumInetAddress.nextElement();

                if (inetAddress.isSiteLocalAddress()) {
                    ip += "Server running at : "
                            + inetAddress.getHostAddress();
                }
            }
        }

    } catch (SocketException e) {

        // TODO Auto-generated catch block
        e.printStackTrace();
        ip += "Something Wrong! " + e.toString() + "\n";
    }
    text.setText(ip);
    return ip;
}

I'm using static port 8080 [May be this is a problem,please suggest me a option if so].

I'm using this lines at client side to connect but soket object returns null on different network:

InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

socket = new Socket(serverAddr, SERVERPORT);

So,Please suggest me what should i use for communicating over different real android device with different network [app runs on same network perfectly]?

Community
  • 1
  • 1
Smit.Satodia
  • 597
  • 4
  • 13
  • Road to disaster. You'll probably always have to configure a router to do that. And 8080 is mostly routed to some HTTPS host. IMHO your best bet is to have a common server in the public internet to connect to from both devices. – Fildor Jun 24 '16 at 08:30
  • @Fidor thanks for replying i have nothing to do with router, please explain me about common server – Smit.Satodia Jun 24 '16 at 08:33
  • The problem with handhelds is, you cannot know in whose network you are. In your own network, you can configure the router to route a port to your device which accepts connections at that port. You cannot possibly configure for example a public access point or mobile network to do so. So you need a public host in the internet to connect to from anywhere. Then you have duplex communication to that server. The latter then has to forward messages from one connection to the other. – Fildor Jun 24 '16 at 08:37
  • @Fildor okay.Let me find out some other option for socket operation on different device and different network, as i am done with half of the project. – Smit.Satodia Jun 24 '16 at 09:57

1 Answers1

0

https://en.wikipedia.org/wiki/TCP_hole_punching

This technique can be used if you're attached to P2P connectivity.

Otherwise, you can get an online server that is accessible to both clients and it can be used to forward messages between the two. Amazon Web Services is a good option for this, you can get one year free trial with a T2.mini machine with a public IP.

https://aws.amazon.com/

Kariem
  • 750
  • 5
  • 13