Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: IP Address

  1. #1
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts

    IP Address

    To make things short, I need to write the IPs of a bunch of sites (inputed by the user), and they need to be Strings. How do I parse InetAddress as String?
    Last edited by Scriptr; 12-23-2011 at 07:06 AM.

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,718
    Thanks
    41
    Thanked 191 Times in 190 Posts
    it is not a "direct" answer but it should lead you to one- post back if you cannot figure it out or need clarification.

    Thread

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by alykins View Post
    it is not a "direct" answer but it should lead you to one- post back if you cannot figure it out or need clarification.

    Thread
    I read through it and searched through it, but I failed to find a variation of Integer.toString(int); for use with InetAddress, and I only found InetAddress in one response. So yes, please, could you clarify?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by Scriptr View Post
    I read through it and searched through it, but I failed to find a variation of Integer.toString(int); for use with InetAddress, and I only found InetAddress in one response. So yes, please, could you clarify?
    I'm confused by what you need. If you provide it a string address of 127.0.0.1 for example, use a InetAddress.getByName() will either create an InetAddress object or fail and throw an exception.
    To go from an InetAddress into an octets approach, you can use the .getAddress to return a byte[]. You can also pull it from the .toString method. Tokenize it on a / since it will return the hostname / ipaddress combination.

  • #5
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    I'm confused by what you need. If you provide it a string address of 127.0.0.1 for example, use a InetAddress.getByName() will either create an InetAddress object or fail and throw an exception.
    To go from an InetAddress into an octets approach, you can use the .getAddress to return a byte[]. You can also pull it from the .toString method. Tokenize it on a / since it will return the hostname / ipaddress combination.
    Code:
    InetAddress redditIP = InetAddress.getByName("reddit.com");
    		redditIP.split("/");
    FileWriter w = new FileWriter(new File(C:\\Windows\\System32\\drivers\\etc\\hosts));
    w.append(redditIP[0] + "\t" + redditIP[1] + "\n");
    w.close
    Problem: you can only split a String; no InetAddress. Also, you can only write String; no InetAddress. So I have to use the variation of InetAddress.toString(redditIP) that actually exists. (I just made that up because it is a logical place-holder until I know what I am really supposed to put there). I don't need reddit; that is just my example.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    And that makes sense. If you want to treat a octet format ip address as an InetAddress, then no its not a string and therefore cannot be operated on as if it were a string. You can split it using inetaddressobj.toString().split("/"); or use the tokenizer. A byte[] will also be provided if you request the .getAddress on the object.

    Also, you can use the .getHostAddress to try and get the textual representation of the ip. The getAddress is actually the hardest to use since for whatever reason they decided to use a byte[], and since there is no unsigned type in java requires a cast to a larger size and a bitmask to display.


  •  

    Posting Permissions

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