简体   繁体   中英

How to find specific desktop's ip address on network?

I'm coding an app which consist of two pieces. Desktop and android. There is one desktop and several android devices. (don't know the count.) I want to communicate android devices between desktop with TCP. However, android devices doesn't know desktop's lan ip address.

I thouht 2 ways:

1-Desktop app changes the local ip address on start. So android devices know the ip address. (I coded with that ip address)

2-Desktop app always tries to connect ip addresses (192.168.0.1 - 192.168.0.255) to sent desktop's ip address. And when an android device connect to the network accept the connection then know desktop's ip.

But there is some problems in both ways.

On first, you must be administrator to changing lan ip. So run command as admin with java is a problem. Because if I do this, when user start the program, uac always asks for it.

On second, I think there will be performance issues because of app always tries to connect. Exept this, when android device connect and dhcp gives it 192.168.0.5 , but loop is on 192.168.0.150. So android device have to wait for connection.

Is there a better way than these?

Look at this post Network discovery in Java using multicasting I think this would be the best way to do it.

  1. The server will listen for a broadcast message from client
  2. the client sends a broadcast request asking for server ip
  3. server receives request and replies back with server ip.

You can use the hostname. If the network is properly configured, the host name will point to the correct ip even if it changes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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