Question

I am struggling with this for three days now. It is an android client application that connects to the server AIR application through Wifi on local network. I need my application to connect to the server every time it is launched without asking the user to enter local IP, in case it changed. It seems there are many ways to make sure the connection is successful, but I want to make sure I can go without the help of RTMFP or PHP or SharedObject stuff. I am able to get local IP using NetworkInterface and NetworkInfo ANE from Android client. Then I use the variable to mySocket.connect(ipAddress, 8888);

But there is a problem: 1) When debugging on Android via Wifi The detected IP 192.168.137.2 2) When debugging on Flash, on computer The detected IP 192.168.137.3 So, the local IPs are not the same. So client fails to connect. Everything works perfect, if I manually set that IP, but I need a code that works, even if the IP on local network changed.

As I mentioned making a textInput field in case IP changed and ask the user manually enter IP is not an option.

My question is why using "localhost" as host parameter of socket.connect(host, port) does not work? If it did, there would be no need for detecting local IP at all. "localhost" works for me only if the client is running on computer, but not Android. Is it the problem with Security Policy file? if so, I have no idea how to use that. I can't find any tutorial on that.

Was it helpful?

Solution

You have two devices 1. Android device running an app and acting as a client 2. Computer running an AIR app and acting as a server

Each of those devices will have it's own IP address on the network. In your case android device IP is 192.168.137.2, and computer IP is 192.168.137.3.

In order for client to connect to server there must to be some means of delivering server IP to the client. Normally for an app like this you enter server IP address manually in settings, or have one central place available on the net where server registers it's IP address and client fetches the address from there.

Since you don't want to use any of these options the last resort would be implementing local area network scanning. Where you scan all the segment 192.168.137.* and search for opened port reserved for server. That of course is not good implementation but could work if project is for your own use.

Lastly "localhost" is name mapped to loopback IP 127.0.0.1 which is special address and is used to connect to the same device app is running on. In your case if android app is connecting to localhost it is trying to connect to the android device itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top