Search code examples
javaandroidlocalhostlan

Will binding the server to localhost not require its reset after network change?


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?


Solution

  • 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.