I've installed sl4a and Python on my Android-powered Galaxy Tab. I've run into problems using python's socket module to communicate between my laptop and my phone.
This example had always worked fine for me when I'd been writing computer-only python programs, but doesn't work as expected on a computer/Tab set-up. Here is the code I'm running on the Tab:
import socket, android
droid=android.Android()
droid.makeToast('Running...')
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 50111))
droid.makeToast('Socket has been bound')
s.listen(1)
conn, addr = s.accept()
droid.makeToast('Connection has been accepted')
And the code I'm executing on the laptop (where <Tab IP Address> is the IP address of the Tab):
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('<Tab IP address>', 50111))
The client script (on the laptop) errors with:
socket.error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
And the script on the Tab displays all but the last Toast message.
I'm getting the IP address of the Tab from www.whatismyip.org.
Local socket programming (i.e. connecting to IP address 'localhost' and running both client and server on the Tab) works fine.
I've been using the guide here to transfer files to my Tab.
For context, the final desired goal of my project is to be able to send a text via my Tab by executing a command on my laptop. I'm sure solutions already exist, but I'd like to learn how to do it myself!
EDIT: [Added 21/03/12] It turns out that the code above works fine (communicating between Tab and Laptop) if I connect the Tab to my wifi network and use the 192.168.0.xx address. This leads me to believe that the problem lies with the IP address I'm using to connect to the Tab, rather than the code itself. I'll keep trying. Any advice on the best way find the IP address to connect to, or errors I might be making, would be appreciated.
EDIT AGAIN: From reading this question and this link, I've concluded that smartphones' (and tablets') IP addresses are not static, and so treating the mobile device as the server in this situation is the wrong way to do it. I'll rethink my program structure.
Although I know now that this was the wrong way to structure the interaction, I still don't entirely understand why it failed. I suspect, from reading the earlier links, that the IP address I was getting from www.whatismyip.org wasn't unique to my Tab, but was rather a "bulk IP" belonging to my network provider, from which connections can then be forwarded on to individual devices (in the same way that a network router can forward connections to itself on to individual devices on the network). However, I'm not certain of this, and can't think of any way to verify it (short of hunting down people on the same network as me, who live in the same cell area, and asking them their mobile IP's). If someone could clarify this point, I'd be very grateful.
You were right about the "bulk IP" thing. When you're connecting your phone through wi-fi it will get a random IP from the router's available IP address pool.
It usually gets the same IP if you have a constant number of devices connected to the router. For example if you have 3 computers connected through cable and 1 smartphone then the phone will always get the 192.168.1.5 IP (192.168.1.2 - .4 IPs are used for your computers).
Depending on the router you have you can set it up to give the exact same address to your smartphone every time you connect it to wi-fi. And the IP you were using from www.whatismyip.org is the one from the provider, which a network uses to reach "public internet" (but that's another story).
You should check your local IP that's on your tablet (Settings->Wi-Fi->Wi-Fi settings-> and press the connection. A small info box should appear with your IP).
You should use that in your code for the server.
I hope this helped.
Related
volley is not working when i want to fetch data from a system on my local network.
where URL is like http://192.168.x.x/data.php
it works well on emulator but not on a real device. it show a time our error.
even i tried setting a maximum time our and retry policy. but in vain.
There are two ways to solve this:
a) keep your device connected to the PC which is connected to your local network either using lan or wifi. Keep debugging mode on. Use ipconfig to check your pcs ipv4 address and use that.
forward your server's port on your router and then use the ipv4 address found on your pc's ipconfig command.
c) Best option-> i use this. Forward your pc's server port on your router, Go to your router's DHCP configurations and give your pc mac a preferred DHCP ip to make sure your router always gives that local ip to your pc(global will still be dynamic if you dont have a static ip). Register to a DDNS and use the set ip above and you will be able to access your web service from anywhere in the world. (i use dynudns for the ddns part)
see How can I access my localhost from my Android device? for other ideas
In the first two steps, make sure that the mobile is connected via wifi so that the router can forward it requests. in third, it can be connected to the internet anyway it likes. Also in the first, you will have to change the url everytime your local ip changes.
In second option, the dns will give a tool for auto ip updation :)
I am trying to debug my visual studio 2012 asp.net project from my phone and have followed all steps here and here.
However none of them work, can't even connect from my other laptop. When I add my public IP or use * IISExpress doesnt even seem to pick up on it (Can't find it in IISExpress->show all applications). But When I used my local IP at least it showed up there and I could connect to it from my own computer, but no other computer/phone.
The cmd commands are successful, I add the extra binding, I open the firewall. Dont know what is wrong.
Which IP (local/public) should I use? what should I type in as URL when I want to test it? I am trying http://IP:port and http://IP:port/Default.aspx.
EDIT: Using this solution I got it to work on my laptop, however I still can not get it to work on my phone.
Basically, the points mentioned in the links you have cited are enough for enabling communication with the phone. But here is one point to make sure (since you have not mentioned it): You have to make sure that both the server and the clients (in your case, the machine that runs the visual studio and your phone and the other laptop) are on the same wireless network. To find out the IP that your server/development machine is currently using, you can issue ipconfig command in cmd and look at the Wireless Network Adapter IPV4 address. For example, if your laptop IP address is 192.168.1.2, you should use http://192.168.1.2:8080/Default.aspx (assuming that your site runs on the port 8080).
You use the local IP if you're on the same network as your server (for example, use WiFi on your phone and join the same network that your server is on).
You use the public IP if the device is outside your network, and you have to configure the firewall to forward the correct ports to the local IP. That's NAT traversal. This would be if you're using the phone's cellular data connection, or you're using a WiFi or wired network that's different from the one your server is on.
Your URL should always be of the format <protocol>://<IP or DNS name>:<port>/<path>. However, you can omit the IP (and the colon) if you're using HTTP on port 80, or HTTPS on port 443. And you can omit the path if you're accessing the root of the site, and your site has a page correctly configured for the root (either via default document in IIS, or route defaults if you're using routing features).
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.
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.
I'm writing an Android Kiosk application for a closed business environment. My devices are connecting over Ethernet to a local web server to show webpages.
Now the problem I have is, that in the night, the server will be switched off, and if that happens, my DHCP server is off too. If my Android starts up in this situation, it won't get an IP address. My Kiosk application tries to connect to the server all the time (At this point I don't care if it does, when it's in a Power saving mode or not).
To make it more complicated, it's not just one site, my app will run on, so using static IP addresses for the server won't work. I managed to get a DHCP and DNS server, where I can define my server host name, which will be the same at all sites.
BUT, Android has a bug resolving not fully qualified domain names (Link), so my application can't directly connect to the host name (e.g. http://hostname/index.html), but has to resolve the IP address of the hostname manually and then connecting to that IP address. As the only solution I could find was dnsjava (Link), I used that to resolve the IP.
import org.xbill.DNS.*;
InetAddress addr = Address.getByName("hostname");
ipAddress = addr.getHostAddress();
This actually works, BUT here's the thing:
If Android and my starts up with no IP, it of course can't resolve the hostname. However if my DHCP/DNS server starts up, the device won't get an IP address unless I tell him to do so:
runShellCommand("killall dhcpcd");
runShellCommand("ifconfig eth0 192.168.99.99");
runShellCommand("ifconfig eth0 0.0.0.0");
runShellCommand("dhcpcd eth0");
runShellCommand will ... run a shell command and actually works well.
Anyway, this will get a new IP address from the DHCP (proven by the Log files of the DHCP server), but the hostname will not be resolved.
Restarting the app won't work, even using another app for testing (Ping & DNS) won't work.
The only way it works is when in Settings->Ethernet I unclick DHCP and click it again. Somehow this flushes the DNS cache?! But how to do that programmatically?
BTW: I tested to start my App and Android when the DHCP/DNS server is online, it will connect and work and when I switch off the server, my Android always answers DNS lookups with this IP address, even I don't have a local IP anymore.
For me it seems, that Android does do a flush when changing the Settings DHCP to fixed IP and back.
I hope someone can help!
You need to kick the resolver:
runShellCommand("ndc resolver setifdns eth0 \"\" 8.8.8.8 8.8.4.4");
runShellCommand("ndc resolver setdefaultif eth0");
I have small TFTP server running on port 8080. It is an Android application. When I try to connect it while being on Ethernet, it works seamlessly. However, when I switch from Ethernet to Wifi. I get 'Connection refuse' error. Or sometime it displays first page and then when I try to navigate, it crashes with same error. So, what do I need to do if the IP changes ? Do i need to restart the TFTP server ?
Thanks
This looks like a wifi network error due to some radio interference or to some problem with the hardware. The connection refused error is probably the effect to some timeout that get triggered on after a (partially) failed attempt to establish a communication channel. This is why you sometime can see a part of the requested document.
I'm almost sure you are using a wireless router to allow the two device to communicate each other. Did you try a different router and/or a totally different place (a friend's house, for example)?