Miky
Miky

Reputation: 942

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?

Upvotes: 0

Views: 157

Answers (2)

Gray
Gray

Reputation: 116888

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.

Upvotes: 1

MByD
MByD

Reputation: 137362

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.

Upvotes: 1

Related Questions