将服务器绑定到本地主机不需要在网络更改后重置吗?

发布于 2024-12-19 15:08:17 字数 95 浏览 0 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_蜘蛛 2024-12-26 15:08:17

我假设您所问的问题是,绑定到本地主机是否可以使您的应用程序免于重新绑定以在刚刚可用的新接口上提供服务。

简短的回答是否定的。绑定到 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.

自在安然 2024-12-26 15:08:17

即使网络发生变化,本地主机也将可用,并且不会给您带来任何问题。许多服务和应用程序都使用它进行进程间通信(例如媒体播放器)并且它是安全的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文