0

I am trying to establish a (all time) connection between my computer and my android phone. So far I have been able to do so, but I have to manually enter the ipv4 address of the server when connecting to a clien e.g socket = new Socket("server ipv4 here", 999);. However, when the network configuration changes, so does the ip address and I have to go to cmd and find the new server's ip using the ipconfig command. For example, suppose that my computer(server) is connected via ethernet and my phone(client) via WiFi and I have used the ethernet ip to connect my client to the server. Then if I connect my server via WiFi, the ip will change so a client-server connection can't be establish. This can also occur if I move both my computer and phone under a different network.

So my question is, is there a way to get a server ip from a client under the same network?

This is what I have tried to do but fail

InetAddress address = InetAddress.getByName("Computer/server host name");
socket = new Socket(address, 999);`

where "computer/server host name" is the host name gotten when calling InetAddress.getLocalHost().getHostName(); on the server computer.

Thanks!

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Astonvish32
  • 179
  • 2
  • 11
  • You can use bonjour/rendevous protocols to announce the server and find it on the client. You have some info on java libraries http://stackoverflow.com/questions/1233204/are-there-any-other-java-libraries-for-bonjour-zeroconf-apart-from-jmdns – gfelisberto Feb 28 '17 at 22:18
  • 1
    General rule of using a server: Set static IP and/or disable DHCP on the router. – OneCricketeer Feb 28 '17 at 22:36
  • 1
    Use DHCP reservations to assign the same IP address to the same device every time. – Ron Maupin Feb 28 '17 at 22:53

0 Answers0