GeeCON Prague 2014*
The moose likes Sockets and Internet Protocols and the fly likes Relation between Host file & java.net.* ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Relation between Host file & java.net.* ?" Watch "Relation between Host file & java.net.* ?" New topic
Author

Relation between Host file & java.net.* ?

Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 499

In hosts file i found following entries:-


in ifconfig i can see my pc ip address is 10.212.11.228 & Mac Address = 00-18-FE-64-5C-37

For Finding Mac Address of PC by programming i use 2 different java programs & test 4 cases
1. host file remain as it is.
2. In host file only active 1 line which has 127.0.0.1 other line inactive by #, restart network service
3. In host file only active 2nd line which has 10.111.17.3 other line inactive by #, restart network service
4. In host file all lines inactive by #, restart network service

Code 1:-

Results of test cases:-
Code 1, Test 1 output in console:-

Code 1, Test 2 output in console:-

Code 1, Test 3 output in console:-

Code 1, Test 4 output in console:-


code 2:-

Results of test cases:-
Code 2, Test 1 output in console:-

Code 2, Test 2 output in console:-

Code 2, Test 3 output in console:-

Code 2, Test 4 output in console:-


My question is what is relation between host file which present in Linux or Windows & java.net.* ?
If what ever i do change for test above test case, in ifconfig result no changes.
I try to google also but not getting proper way.


Millions saw the apple fall, but Newton asked why.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42047
    
  64
Where do you see a connection between the hosts file and the code? In other words, what (and why) did you expect to change?


Ping & DNS - my free Android networking tools app
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 499

In network setting i keep 'Automatically obtain IP address setting with dhcp' when i test above 2 codes.
in ifconfig i can see my ip address 10.212.11.228 & Mach address 00-18-FE-64-5C-37

But then also i write in host file following line
10.111.17.3 dtp016.galaxy.in
to check that ip.getHostAddress() read original IP address or which i write static address which not actually set in network setting.
Rest of the things i write previous post.
My aim is to
1. understand importance of host file
2. understand how my 2 codes works actually.

Why java program give me ip address 10.111.17.3 when i keep inactive 127.0.0.1 in host file but ifconfig command give me proper ipaddress which is set automatically?
How i can write program like ifconfig which always give me right answer?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42047
    
  64
For starters, the MAC address is not relevant - it is related to the hardware you're using, and is not changeable.

I looked briefly at the two codes, and they look similar - what is the difference between them?
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 499

The result related to Mac address
i got with code1 in test 1 & test 3 but

with code 2 any test case i got same result.
(It print mac address at while loop 1st time & mac is null in 2nd time).
That is the only difference i found.
Why code 1 not give me mac address in each test when
For starters, the MAC address is not relevant - it is related to the hardware you're using, and is not changeable.


In test 1 & 3 i do changes in host file which i mention previous post.
for test 1 host file as below:-


for test 2 host file as below:-


for test 3 host file as below:-


for test 4 host file as below:-


So i think there is something relation between host file with these 2 codes.

when i closely look my code 1 i see declaration

it initialize as below

used for getting Hardware Address


Related to NetworkInterface.getByInetAddress(ip) i found java doc

So question comes why specified address is null?

in code 2 used

it initialize as below:-


related to this i got java doc:-


1 more thing i try to understand after lot of searching...
I am using CentOS in that
/etc/sysconfig/networking/profiles/default
i found 5 files
1. hosts (it is same which found in /etc)
2. ifcfg-eth0 it contains following:-

3.network which contains following:-

4. resolve.conf it contains auto generated text when i choose 'Automatically obtain IP address settings with dhcp'
when i use static ip address i keep empty that file other wise pc get slow.
5.route-eth0 which contains as follows:-
 
GeeCON Prague 2014
 
subject: Relation between Host file & java.net.* ?