简体   繁体   English

Android上的服务器可以从本地网络上的设备访问?

[英]Server on Android reachable from devices not on local network?

I have been trying to set up an app that allows communication between two devices, a server and a client. 我一直在尝试设置一个允许两个设备(服务器和客户端)之间通信的应用程序。 I have established the connection and can send messages between the two as long as they are connected to the same source of internet. 我已经建立了连接,并且只要它们连接到相同的Internet来源,就可以在两者之间发送消息。 When I connect the devices to two different sources, I am not able to establish a connection between the two. 将设备连接到两个不同的源时,我无法在两者之间建立连接。 It works by entering the IP address of the server you want to connect to. 通过输入您要连接的服务器的IP地址来工作。

For example, if my server device is connected to my home Wi-Fi and my client device is connected to the same Wi-Fi router, communication works as supposed to. 例如,如果我的服务器设备连接到家庭Wi-Fi,而我的客户端设备连接到同一Wi-Fi路由器,则通信将按预期进行。 But, when I connect the server to Wi-Fi and my client to cellular data (or different Wi-Fi router), the client is not able to find the server. 但是,当我将服务器连接到Wi-Fi并将客户端连接到蜂窝数据(或其他Wi-Fi路由器)时,客户端无法找到服务器。

From all the searching I've done, I found that it is because my server is bound to my local host. 从所有搜索中,我发现这是因为我的服务器已绑定到本地主机。 So my question is, How can I make my server reachable from anywhere regardless of what network you are connected to? 所以我的问题是, 无论您连接到哪个网络如何都可以从任何地方访问服务器?

I used this tutorial to get my server running: 我使用了本教程来使服务器运行:

http://android-er.blogspot.com/2014/02/android-sercerclient-example-server.html http://android-er.blogspot.com/2014/02/android-sercerclient-example-server.html

The very first comment is a question that asks why it only works on a local network and someone answered saying you have to set up port forwarding on your router. 第一条评论是一个问题,为什么它只能在本地网络上工作?有人回答说您必须在路由器上设置端口转发。 From what I understand, this will work only if the server device is always connected to the same router. 据我了解,这仅在服务器设备始终连接到同一路由器时才有效。 This will not be the case for my app. 我的应用程序不是这种情况。 The server will have a new IP address as the device changes networks. 随着设备更改网络,服务器将具有新的IP地址。 I want this IP address to always be reachable no matter what network the server is connected to. 无论服务器连接到哪个网络,我都希望该IP地址始终可用。

You will need to create a relay server. 您将需要创建中继服务器。 This would be set up using a java application on the PC and it would manage virtual "rooms" that then relay the messages to the clients. 这将使用PC上的Java应用程序进行设置,它将管理虚拟“房间”,然后将消息中继到客户端。 The computer you run your relay server off of will need port forwarding but the clients will not. 从中运行中继服务器的计算机将需要端口转发,而客户端则不需要。 Much of the code from that tutorial could be applied to this concept. 该教程中的许多代码都可以应用于该概念。

See: How to create a java Server that accepts client connections and then build a relay connection for a client pair 请参阅: 如何创建一个接受客户端连接的Java Server,然后为客户端对建立中继连接

If your server is behind a firewall, you will not be able to reach it unless that port is open on the firewall. 如果您的服务器在防火墙后面,则除非防火墙上的该端口打开,否则您将无法访问它。 The only way to avoid this is to have a non-firewalled server. 避免这种情况的唯一方法是拥有非防火墙服务器。

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

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