-2

I am trying to make a peer to peer chat application on local Network. And I need to find and list the names of online users in a list.

We all know that the function getHostName() returns us the host name(DNS name) from the INetAddress. In Windows, it returns PC name. But in linux PC and android phones it returns either the IP address or random alphanumeric non user friendly String like android-xxxxxxxxxx (hope you understand). My question is that how can we set the default host name so that the getHostName() function returns the user friendly name that we set through our application?

adelphus
  • 10,116
  • 5
  • 36
  • 46
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
  • What problem are you trying to solve that requires changing the hostname of the computer? – Joni Aug 15 '15 at 17:28
  • I am trying to make a chat application on local Network. And I need to find and list the names of online users in a list. @Joni – Nabin Bhandari Aug 15 '15 at 17:32
  • You could perhaps try a google search. First result: http://forums.androidcentral.com/virgin-mobile-optimus-v/125231-changing-host-name-phone.html second: http://www.invialgo.com/2012/change-android-hostname/ third: https://play.google.com/store/apps/details?id=com.developersinfo.android.dns.changer&hl=en fourth: http://android.stackexchange.com/questions/37/how-do-i-change-the-name-of-my-android-device – 19greg96 Aug 15 '15 at 17:32
  • @Nabin you could list each device on the network and ask each client (over I guess some form of TCP/IP connection) for a user-set name, and show the user the replies – 19greg96 Aug 15 '15 at 17:34
  • @19greg96 I did both of them in my previous iteration. But I think it is impractical to share IP address because there are DHCP set in modern day routers. – Nabin Bhandari Aug 15 '15 at 17:39
  • A hostname that is set locally is totally useless because other nodes will not know how to resolve it. You need to use the IP address. – Joni Aug 15 '15 at 17:53
  • alright guys, thanks for the suggestions. I don't know how I got 4 down votes in no time. :D – Nabin Bhandari Aug 15 '15 at 17:57

2 Answers2

3

Setting the host name is an Administrative task - ordinary users (and Apps) do not have permission to do this.

As you've seen, Android sets the host name to a random value - it's primarily for networking compatibility where a host name is required, but the value is not really intended for human consumption.

I'm not sure why you need the host name. You seem to be using it as a host identifier - wouldn't it be easier just to ask the user to type a sensible name which you could use for identification purposes. In addition, what do you think would happen if every App decided to try and set the devices' host name?

Your comment about not using IP addresses because of DHCP makes little sense. It seems significantly easier to identify devices by IP and simply map them to a shared list of user-chosen identifiers, rather than trying to use and resolve hostnames.

adelphus
  • 10,116
  • 5
  • 36
  • 46
1

I don't think your clients would have to share any more than the local network, as on windows there is the arp -a command (more on that in this answer), and I'm pretty sure there is an android equivalent. It lists all IP addresses on the lan, so you will only need to connect to each one, and ask for the user-defined name. If a client doesn't answer or the port isn't open, it means they are offline.

Community
  • 1
  • 1
19greg96
  • 2,592
  • 5
  • 41
  • 55