public class VerifierAddress {The package that allows us to manipulate IP addresses is java.net and provides the class InetAdress which contains the methods we're going to work with:
static boolean isCorrect(String adr){
String[] parts = adr.split("\\.");
//The range of a number is between 0 and 255
for(int i = 0 ; i < 4; i++){
//Convert to integer and test
if(new Integer(parts[i])< 0 || new Integer(parts[i])> 255)
//retroune false
return false;
}
//returns true by default
return true;
}
public static void main(String[] args) {
String adr= "192.168.1.3";
System.out.println("adr+" is "+isCorrect(adr));
}
}
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
/*************************************
* @author www.codeurjava.com
* All Rights Reserved
*/
public class Addressing {
public static void main(String[] zero) throws SocketException {
String address = "192.168.1.199/10";
String[] parts = address.split("/");
String ip = parts[0];
int prefix;
if (parts.length < 2) {
prefix = 0;
} else {
prefix = Integer.parseInt(parts[1]);
}
System.out.println("Address =\t" + ip+"\nPrefix =\t" + prefix);
//convert the entire mask to a 32-bit array
int mask = 0xffffffff < < (32 - prefix);
int value = mask;
byte[] bytes_masque = new byte[]{
(byte)(value > > > 24), (byte)(value > > 16 & 0xff), (byte)(value > > 8 & 0xff), (byte)(value & 0xff) };
try {
//masque
InetAddress netAddr = InetAddress.getByAddress(bytes_masque);
System.out.println("Mask =\t" + netAddr.getHostAddress());
/*************************
* Network Address
*/
//Convert IP address to long
long ipl = ipToLong(ip);
//Convert IP to a 32bits
byte[] array bytes_ip = new byte[]{
(byte) ((ipl > > 24) & 0xFF),
(byte) ((ipl > > 16) & 0xFF),
(byte) ((ipl > > 8 ) & 0xFF),
(byte) (ipl & 0xFF)};
//The logical ET between the IP address and the mask
byte[] bytes_reseau = new byte[]{
(byte) (bytes_ip[0] & bytes_masque[0]),
(byte) (bytes_ip[1] & bytes_masque[1]),
(byte) (bytes_ip[2] & bytes_masque[2]),
(byte) (bytes_ip[3] & bytes_masque[3]),
};
//obtained network address
InetAddress adr_reseau = InetAddress.getByAddress(bytes_reseau);
System.out.println("Network Address =\t"+adr_reseau.getHostAddress());
/********************************
*Broadcast address
*/
//network address - inverted mask ~val & 0xff
//invert mask
bytes_masque = new byte[]{
(byte) (~bytes_masque[0] & 0xff),
(byte) (~bytes_masque[1] & 0xff),
(byte) (~bytes_masque[2] & 0xff),
(byte) (~bytes_masque[3] & 0xff),
};
System.out.println("Wildcard Mask) =\t"+InetAddress.getByAddress(bytes_masque).getHostAddress());
byte[] bytes_broadcast = new byte[]{
(byte) (bytes_reseau[0] | bytes_masque[0]),
(byte) (bytes_reseau[1] | bytes_masque[1]),
(byte) (bytes_reseau[2] | bytes_masque[2]),
(byte) (bytes_reseau[3] | bytes_masque[3]),
};
//Broadcast address obtained
InetAddress adrbroadcast = InetAddress.getByAddress(bytes_broadcast);
System.out.println("Broadcast Address =\t"+adrbroadcast.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
public static long ipToLong(String ipAddress) {
long result = 0;
String[] ipAddressInArray = ipAddress.split("\\.");
for (int i = 3; i >= 0; i--) {
long ip = Long.parseLong(ipAddressInArray[3 - i]);
result |= ip < < (i*8);
}
return result;
}
}
Address= 35.204.121.13 Prefix= 10 Mask= 255.192.0.0 Wildcard= 0.63.255.255 Network Address= 35.192.0.0 Broadcast Address = 35.255.255.255 |
import java.net.InetAddress;Runtime:
import java.net.SocketException;
import java.net.UnknownHostException;
public class ServerAddress {
public static void main(String[] zero) throws SocketException {
String LocaleAddress;
InetAddress ServerAddress;
try{
//Local Address
LocaleAdresse = (String) InetAddress.getLocalHost().getHostAddress();
System.out.println("The local address = "+LocaleAddress);
//Server address google
ServerAddress= InetAddress.getByName("www.google.net");
System.out.println("Google server address = "+ServerAddress);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
Local address = 192.168.1.2 Google server address = www.google.net/41.201.128.40 |
Please disable your ad blocker and refresh the window to use this website.