简体   繁体   English

如何将两个Android设备与中央服务器连接?

[英]How do I connect two android devices with a central server?

I'd like to know how a central server works in connecting two devices. 我想知道中央服务器如何连接两个设备。 I'm assuming that when the device application starts up, it should register its IP address and other pertinent information (username) with the server. 我假设设备应用程序启动时,应在服务器上注册其IP地址和其他相关信息(用户名)。 When it wants to connect to another device, it should look to find the address of another device on the server, maybe with a get request. 当它想连接到另一台设备时,它应该寻找服务器上另一台设备的地址,也许带有一个获取请求。 Then set up a to connect to a socket. 然后设置一个以连接到插座。 If the device application closes, it should unregister from the server. 如果设备应用程序关闭,则应从服务器注销。 Is this correct? 这个对吗?

That's pretty much correct. 这是非常正确的。

Because one or both devices are probably behind firewalls (including NAT), you have to assume they won't actually be able to connect directly to each other, so it won't be as simple as opening a socket to the other device once you find out its registered address. 由于一个或两个设备可能位于防火墙(包括NAT)之后,因此您必须假定它们实际上无法直接相互连接,因此,这不会像打开另一个设备的套接字那样简单找出其注册地址。 You will either have to try firewall-traversal techniques (which will usually be successful with UDP but not with TCP) or have a helper that isn't behind a firewall (which could be the same as the registration server or something else) carry all the data between the devices that wish to communicate. 您要么必须尝试使用​​防火墙穿越技术(通常在UDP上才能成功,而在TCP上却无法成功),要么使用不在防火墙后面的帮助程序(可能与注册服务器或其他东西相同)来完成所有任务希望通信的设备之间的数据。

Also, you will want to have the registration server time out registrations and the clients periodically refresh them, because clients won't always have a chance to deregister themselves on the server when they terminate or lose access to the network. 另外,您将希望注册服务器超时注册,并且客户端定期刷新它们,因为当客户端终止或失去对网络的访问权限时,客户端不会总是有机会在服务器上注销自身。

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

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