In this section we cover and explain Get a HostName Of IPAddress in details for freshers and experienced
| previous | Home | Next |
Example
package java.net;
import java.util.StringTokenizer;
public class InetAddressFactory {
// Use a byte array like {(byte) 199, (byte) 1, (byte) 32, (byte) 90}
// to build an InetAddressObject
public static InetAddress newInetAddress(byte addr[])
throws UnknownHostException {
try {
InetAddress a = new InetAddress();
a.address = addr[3] & 0xFF;
a.address |= ((addr[2] << 8) & 0xFF00);
a.address |= ((addr[1] << 16) & 0xFF0000);
a.address |= ((addr[0] << 24) & 0xFF000000);
return a;
}
catch (Exception e) { // primarily ArrayIndexOutOfBoundsExceptions
throw new UnknownHostException(e.toString());
}
} // end newInetAddress
// Use a String like 199.1.32.90 to build
// an InetAddressObject
public static InetAddress newInetAddress(String s)
throws UnknownHostException {
// be ready for IPv6
int num_bytes_in_an_IP_address = 4;
byte addr[] = new byte[num_bytes_in_an_IP_address];
StringTokenizer st = new StringTokenizer(s, ".");
// make sure the format is correct
if (st.countTokens() != addr.length)
{
throw new UnknownHostException(s
+ " is not a valid numeric IP address");
}
for (int i = 0; i < add.length; i++) {
int thisByte = Integer.parseInt(st.nextToken());
if (thisByte < 0 || thisByte > 255) {
throw new UnknownHostException(s
+ " is not a valid numeric IP address");
}
// check this
if (thisByte > 127) thisByte -= 256;
addr[i] = (byte) thisByte;
} // end for
return newInetAddress(addr);
} // end newInetAddress
} // end InetAddressFactory
| previous | Home | Next |