简体   繁体   中英

Socket cannot connect to server via mobile internet because I use hostname instead of IP but works in i.e. Eclipse and via WIFI

I have problem with connection between Android phone and server on Raspberry Pi via mobile internet. I created simple server on RPi and Client on android. I also instaled Weaved software to DNS problem. When I use simple client in another PC in ie Eclipse I can connect to server using ie:

client = new Socket("proxy.11.xxx.net", 54812)

The same is on android when I use WIFI. Problem starts when I switch off WIFI. Socket dosn't connect to server. I set permission:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

There is no any exceptions. Socket created successful but doesn't connect to server.

IE I put

proxy.11.xxx.net:54812

to web browser and enter, there is connection to server. Ofcourse it works when I am in local network via WIFI and put straight IP number of RPi but it isn't what I mean. I want to be able connect via Internet from android. Why I can connect the same address from PC and on android not?

Anyone can help/give advice?

EDIT:

When I use: new Socket (proxy.11.xxx.net,54812) there is no Exception. Socket is created ok: http://imgur.com/pTHXaAE

When I use new Socket(http://proxy.11.xxx.net,54812) :

java.net.UnknownHostException: Unable to resolve host "http://proxy11.yoics.net": No address associated with hostname
06-10 22:03:46.576 29922-29944/com.mmc.mateusz.rpilight W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:457)
06-10 22:03:46.576 29922-29944/com.mmc.mateusz.rpilight W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
06-10 22:03:46.576 29922-29944/com.mmc.mateusz.rpilight W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:215)
06-10 22:03:46.576 29922-29944/com.mmc.mateusz.rpilight W/System.err:     at java.net.Socket.tryAllAddresses(Socket.java:109)
06-10 22:03:46.576 29922-29944/com.mmc.mateusz.rpilight W/System.err:     at java.net.Socket.<init>(Socket.java:178)
06-10 22:03:46.576 29922-29944/com.mmc.mateusz.rpilight W/System.err:     at java.net.Socket.<init>(Socket.java:150)
06-10 22:03:46.576 29922-29944/com.mmc.mateusz.rpilight W/System.err:     at com.mmc.mateusz.rpilight.Client.openSocket(Client.java:50)
06-10 22:03:46.576 29922-29944/com.mmc.mateusz.rpilight W/System.err:     at com.mmc.mateusz.rpilight.Client.doInBackground(Client.java:76)
06-10 22:03:46.576 29922-29944/com.mmc.mateusz.rpilight W/System.err:     at com.mmc.mateusz.rpilight.Client.doInBackground(Client.java:30)
06-10 22:03:46.576 29922-29944/com.mmc.mateusz.rpilight W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
06-10 22:03:46.576 29922-29944/com.mmc.mateusz.rpilight W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-10 22:03:46.576 29922-29944/com.mmc.mateusz.rpilight W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-10 22:03:46.576 29922-29944/com.mmc.mateusz.rpilight W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-10 22:03:46.576 29922-29944/com.mmc.mateusz.rpilight W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-10 22:03:46.576 29922-29944/com.mmc.mateusz.rpilight W/System.err:     at java.lang.Thread.run(Thread.java:818)
06-10 22:03:46.577 29922-29944/com.mmc.mateusz.rpilight W/System.err: Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
06-10 22:03:46.577 29922-29944/com.mmc.mateusz.rpilight W/System.err:     at libcore.io.Posix.android_getaddrinfo(Native Method)
06-10 22:03:46.577 29922-29944/com.mmc.mateusz.rpilight W/System.err:     at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
06-10 22:03:46.577 29922-29944/com.mmc.mateusz.rpilight W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:438) 

This is clearly a networking issue, not a coding issue.

In order to access the RPi over the Internet, you need a public Internet IP address that can route traffic to the RPi. And if you want clients to connect using a static hostname instead of the IP directly, you need a DNS registration (in this case, provided by weaved.com ) that maps the desired hostname to the current IP address.

For the IP address itself, the RPi needs either:

  1. its own direct connection to the Internet, so it has a public IP address assigned by your ISP.

  2. to be connected to a LAN that has a public Internet IP address. The LAN router(s) must have port forwarding configured to forward outside traffic to the RPi. Clients will be connecting to the router that is directly connected to the Internet, and then the router(s) will forward the connections to the RPi.

Either way, your ISP must also not be blocking inbound client connections. Some ISPs block customers from hosting their own servers, or they charge extra for that feature. Check with your ISP.

These issues are not a factor when your client app(s) and RPi are on the same side of your LAN, such as when both are connected to the same router via WiFi.

So, in short, make sure you have a public IP address that provides a viable route to exchange data packets between the RPi and outside Internet clients, and make sure you have a static hostname that can be resolved to that IP address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM