View previous topic :: View next topic |
Author |
Message |
trustlix n00b
Joined: 25 May 2004 Posts: 22 Location: Brazil
|
Posted: Tue Oct 18, 2005 2:45 pm Post subject: [SOLVED]Problem when get hostname and domainname in java cod |
|
|
Hy all,
Im having some terrible time with the java code above (Im using the sun-jdk-1.4.2.09):
Code: |
import java.net.InetAddress;
public class Teste {
public static void main(String args[]) throws Exception{
String host = InetAddress.getLocalHost().getHostName();
System.out.println("host- getHostName: "+host);
host = InetAddress.getLocalHost().getCanonicalHostName();
System.out.println("host- getCanonical: "+host);
host = InetAddress.getLocalHost().getHostAddress();
System.out.println("host- getHostAddress: "+host);
host = InetAddress.getByName(InetAddress.getLocalHost().
getHostAddress()).getHostName();
System.out.println("host- getByName: "+host);
}
}
|
When I run this app, I get the following output:
Quote: | $ java Teste
host- getHostName: notexerozo.gentuxxx
host- getCanonical: notexerozo.gentuxxx
host- getHostAddress: 10.10.1.27
host- getByName: notexerozo.gentuxxx
|
But it should be:
Quote: | $ java Teste
host- getHostName: notexerozo
host- getCanonical: notexerozo.gentuxxx
host- getHostAddress: 10.10.1.27
host- getByName: notexerozo.gentuxxx
|
Where the getHostName is just the name of the host, whitout dommain....
Here is my /etc/hosts:
Code: |
127.0.0.1 localhost.localdomain localhost
10.10.1.27 notexerozo.gentuxxx notexerozo
# IPV6 versions of localhost and co
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
|
and the output of the commands:
Quote: | #hostname
notexerozo
#dnsdomainname
gentuxxx
|
I hope anyone nows what's going on here.... thanksss.
Last edited by trustlix on Sun Oct 23, 2005 8:21 pm; edited 1 time in total |
|
Back to top |
|
|
kloune Apprentice
Joined: 09 May 2004 Posts: 185 Location: lost
|
Posted: Wed Oct 19, 2005 10:06 am Post subject: |
|
|
Hi,
Did you try to revers the names in this line ?
Code: |
10.10.1.27 notexerozo.gentuxxx notexerozo
|
Had same problem a while ago in another program, I think that solved it. |
|
Back to top |
|
|
trustlix n00b
Joined: 25 May 2004 Posts: 22 Location: Brazil
|
Posted: Wed Oct 19, 2005 1:36 pm Post subject: |
|
|
Humm...
I tried that trick, but doing this i also lost the canonical-name of my host (that should be notexerozo.gentuxxx).
All I got was the hostname.
I also used some code from a Linux book: a C program that does the same thing, and it worked without problems.
I really don't know if this is a JVM problem, or some compillation issue in my system... I'll keep trying
Thanks.
Code: |
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
char *host, **names, **addrs;
struct hostent *hostinfo;
if(argc == 1) {
char myname[256];
gethostname(myname, 255);
host = myname;
}
else
host = argv[1];
hostinfo = gethostbyname(host);
if(!hostinfo) {
fprintf(stderr, "cannot get info for host: %s\n", host);
exit(1);
}
printf("results for host %s:\n", host);
printf("Name: %s\n", hostinfo -> h_name);
printf("Aliases:");
names = hostinfo -> h_aliases;
while(*names) {
printf(" %s", *names);
names++;
}
printf("\n");
if(hostinfo -> h_addrtype != AF_INET) {
fprintf(stderr, "not an IP host!\n");
exit(1);
}
addrs = hostinfo -> h_addr_list;
printf("IP Address: ");
while(*addrs) {
printf(" %s", inet_ntoa(*(struct in_addr *)*addrs));
addrs++;
}
printf("\n");
exit(0);
}
|
The output was:
Quote: | $./net_information
results for host notexerozo:
Name: notexerozo.gentuxxx
Aliases: notexerozo
IP Address: 10.10.1.27
|
|
|
Back to top |
|
|
trustlix n00b
Joined: 25 May 2004 Posts: 22 Location: Brazil
|
Posted: Sun Oct 23, 2005 8:19 pm Post subject: |
|
|
I think i found the problem.
I use DHCP here, and my /etc/hosts was wrong.
I changed the line:
Code: |
127.0.0.1 localhost.localdomain localhost
|
to
Code: |
127.0.0.1 localhost.localdomain notexerozo localhost
|
Just included the second alias, and now it's working.
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|