将服务器绑定到本地主机不需要在网络更改后重置吗?
我正在 android 上开发一个自动交换 LAN 网络的应用程序,并且我有一个服务器应该侦听某个端口的连接。将其地址绑定到本地主机是否可以让我免去重置服务器并再次启动的麻烦?
I am developing an application on android which automatically swaps LAN networks, and I have a server which should listen on some port for connections. Will binding its address to localhost save me from the trouble of reseting the server and starting it again?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您所问的问题是,绑定到本地主机是否可以使您的应用程序免于重新绑定以在刚刚可用的新接口上提供服务。
简短的回答是否定的。绑定到 localhost 仅意味着您绑定到环回接口。如果发生网络更改(例如 wifi 连接出现或消失),环回接口不会受到影响,您将必须重新绑定才能获取新接口。
请参阅此处了解一些想法: 检测新的网络连接(linux服务器)及其在java中的状态
我想你需要轮询NetworkInterface.getInterfaceAddresses() 经常查看是否有新接口。
I'm assuming that what you are asking if binding to localhost saves your application from needed to rebind to serve on a new interface that just became available.
The short answer is no. Binding to
localhost
only means you are binding to the loopback interface. If a network change happens (like a wifi connection comes or goes) the loopback interface is not affected and you will have to rebind to get the new interface.See here for some ideas: Detect a new network connection (linux-server) and it's status in java
I guess you need to poll NetworkInterface.getInterfaceAddresses() every so often to see if there is a new interface.
即使网络发生变化,本地主机也将可用,并且不会给您带来任何问题。许多服务和应用程序都使用它进行进程间通信(例如媒体播放器)并且它是安全的。
Localhost will be available even at network change, and should make no problem for you. Many services and applications are using it for inter-process communication (such as media player) and it is safe.