Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-23-2011, 04:58 AM   PM User | #1
Scriptr
Regular Coder

 
Join Date: Oct 2011
Posts: 106
Thanks: 12
Thanked 0 Times in 0 Posts
Scriptr is an unknown quantity at this point
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..
Scriptr is offline   Reply With Quote
Old 12-23-2011, 03:39 PM   PM User | #2
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
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
alykins is offline   Reply With Quote
Old 12-23-2011, 08:19 PM   PM User | #3
Scriptr
Regular Coder

 
Join Date: Oct 2011
Posts: 106
Thanks: 12
Thanked 0 Times in 0 Posts
Scriptr is an unknown quantity at this point
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?
Scriptr is offline   Reply With Quote
Old 12-23-2011, 08:41 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 12-23-2011, 08:52 PM   PM User | #5
Scriptr
Regular Coder

 
Join Date: Oct 2011
Posts: 106
Thanks: 12
Thanked 0 Times in 0 Posts
Scriptr is an unknown quantity at this point
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.
Scriptr is offline   Reply With Quote
Old 12-23-2011, 09:05 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:47 AM.


Advertisement
Log in to turn off these ads.