...

View Full Version : IP Address



Scriptr
12-23-2011, 04:58 AM
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?

alykins
12-23-2011, 03:39 PM
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 (http://www.codingforums.com/showthread.php?t=245688)

Scriptr
12-23-2011, 08:19 PM
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 (http://www.codingforums.com/showthread.php?t=245688)

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?

Fou-Lu
12-23-2011, 08:41 PM
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.

Scriptr
12-23-2011, 08:52 PM
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.


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.

Fou-Lu
12-23-2011, 09:05 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum