Page 1 of 2 12 LastLast
Results 1 to 20 of 24
Like Tree1Likes

Thread: How to return your IP address?

  1. #1
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default How to return your IP address?

    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.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default 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().

  3. #3
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default 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

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: How to return your IP address?

    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default 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 02:57 PM. Reason: code tags added

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default 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());

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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.

    ** This space for rent **

  8. #8
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default 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

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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.

    ** This space for rent **

  10. #10
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default 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

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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.

    ** This space for rent **

  12. #12
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default 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

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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.

    ** This space for rent **

  14. #14
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: How to return your IP address?

    Oh right I see, sure thanks for your help anyway.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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.

    ** This space for rent **

  16. #16
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default 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.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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).
    dougie1809 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  18. #18
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: How to return your IP address?

    Quote Originally Posted by dougie1809 View Post
    non-static method getHostAddress() cannot be referenced from a static context
    Did you try to find out what it really means?

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: How to return your IP address?

    Quote Originally Posted by Tolls View Post
    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.
    As far as I know it is not possible with Java, but you can manage them through sockets. Ports availability and all handles by the OS itself usually.

  20. #20
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: How to return your IP address?

    Quote Originally Posted by Eranga View Post
    Did you try to find out what it really means?
    Not really, I only got around how to return the IP address from the host name.
    Im assuming it means that when I try call the getHostAddress() method, the compiler thinks that im calling it from a static variable, which im not. So thats why I was confused.

Page 1 of 2 12 LastLast

Similar Threads

  1. Getting IP address
    By GaneshB in forum Networking
    Replies: 11
    Last Post: 01-26-2012, 11:26 AM
  2. set IP address
    By yolimau in forum Networking
    Replies: 10
    Last Post: 05-30-2010, 03:37 AM
  3. Client IP Address
    By goodjonx in forum Networking
    Replies: 3
    Last Post: 09-23-2009, 11:27 AM
  4. JSP getting IP address
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-29-2008, 10:05 AM
  5. MAC address capturing
    By viswa.tk in forum Java Applets
    Replies: 0
    Last Post: 12-13-2007, 09:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •