PDA

View Full Version : socket problems


liquid
09-10-2002, 05:25 PM
Hi. I am having problems getting this to work.

import java.net.*;
public class GetIP {
public static void main(String[] args) {
if ( (args.length == 0) || (args.length > 1) ) {
System.out.println("Usage: java GetIP <hostname> ");
System.exit(0);
}

String host = args[0];

try {
InetAddress inet = InetAddress.getByName("host");
}
catch(Exception e) {
System.out.println("Error");
System.exit(0);
}

System.out.println("IP : " + inet.GetHostAddress());

}
}


i want it to take the hostname from the commandline and return the IP.
The error I get is:

GetIP.java:19: cannot resolve symbol
symbol : variable inet
location: class GetIP
System.out.println("IP : " + inet.GetHostAddress());

1 error

kmj
09-10-2002, 09:36 PM
no prob, dude, you're declaring the variable inside the try block, so it goes out of scope before you try to use it.. to fix, try this...


import java.net.*;
public class GetIP {
public static void main(String[] args) {
InetAddress inet = null;
if ( (args.length == 0) || (args.length > 1) ) {
System.out.println("Usage: java GetIP <hostname> ");
System.exit(0);
}

String host = args[0];

try {
inet = InetAddress.getByName("host");
}
catch(Exception e) {
System.out.println("Error");
System.exit(0);
}

System.out.println("IP : " + inet.GetHostAddress());

}
}

inkedmn
09-10-2002, 11:32 PM
ok, this WORKS <ahem>kmjsucks<ahem> ;) ;)


import java.net.*;
public class GetIP {
public static void main(String[] args) {
InetAddress inet = null;
if ( (args.length == 0) || (args.length > 1) ) {
System.out.println("Usage: java GetIP <hostname> ");
System.exit(0);
}

String host = args[0];

try {
inet = InetAddress.getByName(host);
}
catch(Exception e) {
System.out.println("Error");
System.exit(0);
}

System.out.println("IP : " + inet.getHostAddress());

}
}


there were only a couple little problems, but it worky now :)

kmj
09-10-2002, 11:52 PM
dude you can't even close your xml tags.

And considering I was copying liquid's code, I think you just insulted him!

liquid, I wouldn't take that sheet, man! I'd fire back! I'll privmsg you the inked man's IP and r00tpsswrd so you can 1337h4xXzo0rR him...

liquid
09-11-2002, 03:21 AM
THANKS! It works perfectly now.

I have just begun learning java and I think its a great language..
It's much easier than c/c++.

thanks again.