InetAddress and inetsocketaddress look similar in Java to describe IP addresses and host names. Of course, they also support the use of regular methods to check the address: Loopback address, local address, multicast address, basic return method: Get IP, get host name, etc.
The important thing is that inetsocketaddress contains inetaddress. This means that if we want to do anything with the inetaddress in inetsocketaddress, we just need to get it through the Getinetaddress () method. Comparison Table
Property |
inetaddress |
inetsocketaddress |
Description Object |
IP Address |
Socket address (IP address + port) |
Describe |
IP and host object names |
The IP and host object names, including the port number |
Solve the problem |
IP to host name, host name to IP |
IP to host name, host name to IP, can contain port |
Get Object |
Inetaddress.getlocalhost (); Inetaddress.getbyname (String); Inetaddress.getbyaddress (String); |
Inetsocketaddress.createunresolved (String, Port); |
code Example
public class Hotdream {public
static void Main (String args[]) {
byte[] b = new byte[] {(byte), (Byte), (b Yte) 1, (byte) 1};
try {
inetaddress add = inetaddress.getbyaddress (b);
System.out.println (Add.tostring ());
Add = Inetaddress.getbyname ("localhost");
System.out.println (Add.tostring ());
Inetsocketaddress Addsock = inetsocketaddress.createunresolved (
"localhost",);
System.out.println (Addsock.tostring ());
} catch (Exception e) {
}
}
}
Original Connection
https://commitflame.wordpress.com/2010/12/22/comparing-inetaddress-and-inetsocketaddress-in-java/