简体   繁体   English

套接字无法通过移动互联网连接到服务器,因为我使用主机名而不是IP,但可以在Eclipse和WIFI中使用

[英]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. 我通过移动互联网无法连接Android手机和Raspberry Pi上的服务器之间的连接。 I created simple server on RPi and Client on android. 我在RPi和Android上的Client上创建了简单的服务器。 I also instaled Weaved software to DNS problem. 我还安装了Weaved软件解决DNS问题。 When I use simple client in another PC in ie Eclipse I can connect to server using ie: 当我在Eclipse的另一台PC中使用简单客户端时,我可以使用以下方法连接到服务器:

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

The same is on android when I use WIFI. 使用WIFI时在android上也是如此。 Problem starts when I switch off WIFI. 当我关闭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 IE浏览器

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. 当然,当我通过WIFI进入本地网络并输入RPi的IP地址时,它确实有效,但这不是我的意思。 I want to be able connect via Internet from android. 我希望能够从Android通过Internet连接。 Why I can connect the same address from PC and on android not? 为什么我可以从PC连接同一地址,而不能在android上连接?

Anyone can help/give advice? 任何人都可以帮助/提供建议吗?

EDIT: 编辑:

When I use: new Socket (proxy.11.xxx.net,54812) there is no Exception. 当我使用: new Socket (proxy.11.xxx.net,54812) ,没有异常。 Socket is created ok: http://imgur.com/pTHXaAE 套接字创建成功: http : //imgur.com/pTHXaAE

When I use new Socket(http://proxy.11.xxx.net,54812) : 当我使用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. 为了通过Internet访问RPi,您需要一个公共 Internet IP地址,该地址可以将流量路由到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. 而且,如果您希望客户端使用静态主机名而不是直接使用IP进行连接,则需要将所需的主机名映射到当前IP地址的DNS注册(在这种情况下,由weaved.com提供)。

For the IP address itself, the RPi needs either: 对于IP地址本身,RPi需要:

  1. its own direct connection to the Internet, so it has a public IP address assigned by your ISP. 直接连接到Internet,因此它具有由ISP分配的公共IP地址。

  2. to be connected to a LAN that has a public Internet IP address. 连接到具有公共Internet IP地址的LAN。 The LAN router(s) must have port forwarding configured to forward outside traffic to the RPi. LAN路由器必须将端口转发配置为将外部流量转发到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. 客户端将连接到直接连接到Internet的路由器,然后路由器会将连接转发到RPi。

Either way, your ISP must also not be blocking inbound client connections. 无论哪种方式,您的ISP也一定不能阻止入站客户端连接。 Some ISPs block customers from hosting their own servers, or they charge extra for that feature. 一些ISP阻止客户托管自己的服务器,或者他们为此功能收取额外费用。 Check with your ISP. 请与您的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. 当您的客户端应用程序和RPi位于LAN的同一侧时(例如,两者都通过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. 因此,简而言之,请确保您有一个公共IP地址,该地址提供了在RPi与外部Internet客户端之间交换数据包的可行路由,并确保您有一个可以解析为该IP地址的静态主机名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过主机名连接到Java套接字 - Connect to Java socket via hostname 无法通过Pentaho连接到Kognitio-无法为IP错误创建套接字 - Unable to connect to Kognitio via Pentaho - Cannot create socket for IP Error 在设备上未安装任何应用程序的情况下,通过低功耗蓝牙(即通过信标)向附近的移动设备发送通知 - Send notifications to nearby Mobile Devices through Bluetooth Low Energy(i.e. via beacons) with no app installed on their device 当连接到具有许多线程的远程JVM时(即通过TCPIP传输线程调试信息),Java的JDB(和Eclipse)会挂起 - Java's JDB (and Eclipse) hang when connecting to remote JVMs with many threads (i.e. transmitting thread debug information via TCPIP) 我可以在JDBC连接器中使用数据库IP地址代替主机名吗? - Can I use database IP address instead of hostname in JDBC connector? 无法通过实际IP访问Eclipse Google App Engine服务器 - Eclipse Google App Engine Server cannot be reached via the actual IP 通过Android中的套接字连接到服务器 - Connect to server via socket in Android 如果我使用移动数据而不是wifi,Android无法运行 - Android doesn't work if I use mobile data instead of wifi Java RMI 在 LAN 中工作时无法通过 WAN 连接到服务器 - Java RMI cannot connect to server via WAN while it works in LAN 通过Internet远程连接到Mongodb Server? - Connect to Mongodb Server remotely via internet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM