简体   繁体   English

如何将Java侦听接口从localhost更改为IP地址?

[英]how to change java listening interface from localhost to IP address?

as the title mention, how i can change the java listening interface from localhost to IP address. 如标题所述,我如何将Java侦听接口从localhost更改为IP地址。 since this command netstat -tulpn shows: 因为此命令netstat -tulpn显示:

tcp     0      0 127.0.0.1:7778     0.0.0.0:*     LISTEN      23958/java

I want to change 127.0.0.1 for example to 192.168.1.1 without using sockets, for example to specify it in java configuration files or in jade files. 我想将127.0.0.1例如更改为192.168.1.1,而不使用套接字,例如在java配置文件或jade文件中指定它。

I want to do that in order to make that port reachable to allow the migration of mobile agent from remote machine to this machine. 我要这样做是为了使该端口可访问,以允许移动代理从远程计算机迁移到此计算机。

If you use a ServerSocket , you have to use the three-argument constructor to do that; 如果使用ServerSocket ,则必须使用三参数构造函数来完成; the third argument is then the listening address for this socket. 那么第三个参数是此套接字的侦听地址。

Similar constructors/factory methods exist for ServerSocketChannel s etc. ServerSocketChannel等存在类似的构造函数/工厂方法。

For you to have a listener for an address other than the loopback/localhost address, there has to be an available network interface to listen on. 要使您有一个监听器,而不是回送/本地主机地址,必须有一个可用的网络接口可以监听。 If you don't have additional network adapters on the machine I don't see how you're going to accomplish your task. 如果您的计算机上没有其他网络适配器,那么我将看不到如何完成任务。

If you do have additional network adapters, use the NetworkInterface.getNetworkInterfaces() to get all the available adapters the machine has to offer and pick which want you want to set your listener to. 如果确实有其他网络适配器,请使用NetworkInterface.getNetworkInterfaces()获取计算机必须提供的所有可用适配器,并选择要将监听器设置为哪个适配器。

This SO thread should give you some insight on getting the available network adapters. 该SO线程应该使您对获取可用的网络适配器有一些了解。 How to enumerate IP addresses of all enabled NIC cards from Java? 如何从Java枚举所有已启用的NIC卡的IP地址?

If you're wanting to use the localhost as the listener, because you have apps on the local machine that need to talk to your server, why is using the loopback address not an option for you? 如果要使用本地主机作为侦听器,因为本地计算机上有需要与服务器通信的应用程序,为什么不选择使用环回地址呢?

The problem was in the configuration in my /etc/hosts. 问题出在我的/ etc / hosts中的配置中。 so i solve it by configure it correctly 所以我通过正确配置解决它

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

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