2

as the title mention, how i can change the java listening interface from localhost to IP address. since this command netstat -tulpn shows:

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.

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.

Nadya Nux
  • 519
  • 1
  • 5
  • 17

3 Answers3

2

If you use a ServerSocket, you have to use the three-argument constructor to do that; the third argument is then the listening address for this socket.

Similar constructors/factory methods exist for ServerSocketChannels etc.

fge
  • 119,121
  • 33
  • 254
  • 329
  • no im not using serverSocket... I only use a server without interface graphic.... so there is any way to configure java to listen on an IP address instead of localhost? – Nadya Nux Apr 14 '15 at 16:52
  • What does using `ServerSocket` have to do with using a GUI? – fge Apr 14 '15 at 17:26
  • I just gave you information about my environment... i know there is no relation with it :D – Nadya Nux Apr 14 '15 at 17:31
2

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.

This SO thread should give you some insight on getting the available network adapters. How to enumerate IP addresses of all enabled NIC cards from Java?

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?

Community
  • 1
  • 1
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
  • it's not an option for me because im trying to make that port open and listen to foreign mobile agent that migrate from a remote machine to that machine, the only running service is jade that uses java. so running threads and sockets is not an option for me. – Nadya Nux Apr 14 '15 at 18:18
  • Ok... I'm unfamiliar with jade, but still, as soon as you say, "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." keyword being REMOTE machine, this must mean you're on a network of some sort. That is going to require a network adapter to be on that network and the localhost/loopback address is just for the local machine. – Shar1er80 Apr 14 '15 at 18:36
  • yes that is right, that's why i want to change localhost address to the machine IP address, I don't know if that possible to change the listening address of java in java configuration files or something like that. – Nadya Nux Apr 14 '15 at 18:45
1

The problem was in the configuration in my /etc/hosts. so i solve it by configure it correctly

Nadya Nux
  • 519
  • 1
  • 5
  • 17