An IP address of type IPV4 on 32 bits(4 bytes) is in this form: xxx.xxx.xxx.xxx, for example: 192.168.0.3. It is composed of two parts:
A right-hand part indicates the network called Network ID : 192.168.0 and it is static.
The default IP address is 0.0.0.0/8. It is used to connect only to DHCP server, The latter provides the IP address.
There are three classes of addressing:
- the class A: Network ID: xxx (126 networks) and the Host ID: xxx.xxx.xxx (16777214 computers).
- the class B: Network ID : xxx.xxx (16384 networks ) et l'Host ID: xxx.xxx (65534 computers).
- the class C: Network ID : xxx.xxx.xxx (2097152 networks ) et l'Host ID: xxx (254 computer).
Mask:
The Interest of the mask is to allow to identify The network associated with the IP address. It is made up of 4 bytes like an IP address, for example the mask of the IP address 192.168.0.1 is 255.255.255.0 and we write 192.168.0.1/24. For beginners I advise you to read this summary on Wikipedia: IP address.Check if an IP address is correct
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));
}
}
getLocalHost: returns the local IP address of the machine.
getByName(String nom_de_la_machine): Returns the IP that matches the name passed as a parameter.
getAllByName(String nom_de_la_machine): Returns all IP addresses. which match the name passed as the parameter.
The methods applicable to this object returned by any of the described methods are:
getHostName: returns the name of the machine.
getAddress: returns the IP address as an array.
toString: return there Channel containing the name and address.
The following program calculates the prefix, address, netmask, Cisco wildcard, network address, and broadcast address.
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;
}
}
Runtime:
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 |
Know the IP address of the server on the web
It is also possible to find out the local address of the computer and server from a web address.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 |
Commentaires (12)
Connectez-vous pour commenter
Rejoignez la discussion et partagez vos connaissances avec la communauté
Excellent tutoriel !
N'hésitez pas si vous avez des questions.