Hi,
Id like to know how to only return the IP address of your own computer by itself rather then to return both the IP address and the host name? Im using the method getLocalHost() from the InetAddress class.
Thanks.
Printable View
Hi,
Id like to know how to only return the IP address of your own computer by itself rather then to return both the IP address and the host name? Im using the method getLocalHost() from the InetAddress class.
Thanks.
getLocalHost() returns something localhost/xxx.xxx.xxx.xxx (read on Java doc about what it is really). So it is matter of the manipulating that string object. Split it from the back slash.
And also did you read the Java doc? I don't think so. Because getLocalHost() could return InetAddress and read more about getHostAddress().
Yes i have read the Java API, there is no method to split the string object from the InetAddress object. Not that i know of. So thats why im here on this forum page to get as much advise as possible to be able to return only the IP address, thus splitting the two objects or how ever it can be done?
Thanks
Ok. This is a simple program to get the host address of the computer. But after compiling its giving me the error:
non-static method getHostAddress() cannot be referenced from a static context
The program is below as follows: Thanks again
Code:import java.net.*;
public class GetIpAddress
{
public static void main(String args[])
{
String hostAdd;
try
{
hostAdd = InetAddress.getHostAddress();
}
catch(UnknownHostException e)
{
//Do nothing
}
System.out.println(hostAdd);
}
}
or simplier inside a try-catch-block
System.out.println("IP of my system is := "
+ InetAddress.getLocalHost().getHostAddress());
First, read the API.
It tells you getHostAddress is a non-static method.
So you need to (as j2me64 has) getLocalHost() first.
Second, I would suggest not eating exceptions.
Thanks alot guys. That worked perfectly. May I ask one more question which is a little out of topic. Ive asked this question many times, and searched through many sites etc, but no luck. When you create a socket to the local host to a port number, and then you can request that port to see it it is open or not, how can you go about closing that port in java?
Thanks again
There is an API you know:
Socket.close()
No, that closes the socket connection, NOT the port that is opened. See where im coming from? How can you go about closing that port which may be either at a listening or an established state?
Thanks
You mean a port that the app itself did not open?
I'm not entirely sure what you're trying to do.
Force open a socket on a port that's in use?
Close a port that's in use?
Yes, close the port that's in use.
See I've written a java program to scan for ports on the local machine which in turn returns ports which are opened. So then I'd like to actually close that port which is in use?
Thanks
I doubt the OS would allow Java to do that.
Java isn't really a tool for that sort of control of stuff that the OS tends to manage.
Oh right I see, sure thanks for your help anyway.
Of course, don't take my word for it.
There is possibly a package somewhere, but it might involve JNI.
Oh, like a package you could get from the internet? Well the only possibility I can think of if I create a batch file to located the PID of the process that opened a port, and to then kill that process. Then a java program could then open that file. But I would know how to go about doing that though.
You could do that via Runtime exec().
It would be OS-specific (or at least the batch file would).