Results 1 to 20 of 24
Thread: How to return your IP address?
- 02-29-2012, 02:40 AM #1
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
- 02-29-2012, 05:27 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: How to return your IP address?
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().
- 02-29-2012, 10:57 AM #3
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: How to return your IP address?
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
- 02-29-2012, 11:09 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: How to return your IP address?
Please do not ask for code as refusal often offends.
- 02-29-2012, 01:27 PM #5
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: How to return your IP address?
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
Java 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); } }Last edited by Eranga; 03-01-2012 at 01:57 PM. Reason: code tags added
- 02-29-2012, 01:42 PM #6
Re: How to return your IP address?
or simplier inside a try-catch-block
System.out.println("IP of my system is := "
+ InetAddress.getLocalHost().getHostAddress());
- 02-29-2012, 01:48 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: How to return your IP address?
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.Please do not ask for code as refusal often offends.
- 02-29-2012, 02:02 PM #8
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: How to return your IP address?
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
- 02-29-2012, 02:16 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: How to return your IP address?
There is an API you know:
Socket.close()Please do not ask for code as refusal often offends.
- 02-29-2012, 05:50 PM #10
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: How to return your IP address?
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
- 02-29-2012, 06:10 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: How to return your IP address?
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?Please do not ask for code as refusal often offends.
- 02-29-2012, 06:17 PM #12
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: How to return your IP address?
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
- 03-01-2012, 09:49 AM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: How to return your IP address?
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.Please do not ask for code as refusal often offends.
- 03-01-2012, 11:44 AM #14
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: How to return your IP address?
Oh right I see, sure thanks for your help anyway.
- 03-01-2012, 11:49 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: How to return your IP address?
Of course, don't take my word for it.
There is possibly a package somewhere, but it might involve JNI.Please do not ask for code as refusal often offends.
- 03-01-2012, 12:05 PM #16
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: How to return your IP address?
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.
- 03-01-2012, 01:12 PM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: How to return your IP address?
You could do that via Runtime exec().
It would be OS-specific (or at least the batch file would).Please do not ask for code as refusal often offends.
- 03-01-2012, 01:58 PM #18
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: How to return your IP address?
- 03-01-2012, 02:01 PM #19
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: How to return your IP address?
- 03-01-2012, 03:30 PM #20
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: How to return your IP address?
Similar Threads
-
Getting IP address
By GaneshB in forum NetworkingReplies: 11Last Post: 01-26-2012, 10:26 AM -
set IP address
By yolimau in forum NetworkingReplies: 10Last Post: 05-30-2010, 02:37 AM -
Client IP Address
By goodjonx in forum NetworkingReplies: 3Last Post: 09-23-2009, 10:27 AM -
JSP – getting IP address
By Java Tip in forum Java TipReplies: 0Last Post: 01-29-2008, 09:05 AM -
MAC address capturing
By viswa.tk in forum Java AppletsReplies: 0Last Post: 12-13-2007, 08:36 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks