4.1 Inetaddress class
4.1 Inetaddress class
Unit Outcome-
1. Use Inet address class to know the IP address of the given host name.
2. Use URL connection classes to read and write data to the specified resources
referred by the given URL
3. Develop program for client/server communication through TCP/IP server sockets
for the given problem
4. Develop program for client/server communication through datagram protocol for
the given problem
Networking classes and Interfaces
Java Networking Classes
CacheRequest CookieHandler
CookieManager Datagrampacket
Inet Address ServerSocket
Socket DatagramSocket
Proxy URL
URLConnection
-encapsulates both numerical IP address and the domain name for that address.
Factory Methods-
Three commonly used Inet Address factory methods are.
1. static InetAddress getLocalHost() throws UnknownHostException
2. static InetAddress getByName (String hostname) throws UnknownHostException
3. static InetAddress[ ] getAllByName (String hostname) throws UnknownHostException
Instance Methods-
1. public String getHostName()- it returns the host name of the IP address.
2. public String getHostAddress()- it returns the IP address in string format.
Develop a program for Inetaddress
import java.net.*;
class Demo
{ Output-
public static void main(String[] args) throws UnknownHostException Welcome-PC/59.161.87.227
{ www.studytonight.com/208.91.198.55
InetAddress address = InetAddress.getLocalHost(); www.google.com/74.125.236.115
System.out.println(address); www.google.com/74.125.236.116
address = InetAddress.getByName("www.studytonight.com"); www.google.com/74.125.236.112
System.out.println(address); www.google.com/74.125.236.113
InetAddress sw[] = InetAddress.getAllByName("www.google.com"); www.google.com/74.125.236.114
for(int i=0; i< sw.length; i++)
{
System.out.println(sw[i]);
}}
}
Develop a program for Inetaddress instance methods
import java.io.*;
import java.net.*; Output-
Host Name: www.javatpoint.com
public class InetDemo{
IP Address: 206.51.231.148
public static void main(String[] args){
try{
InetAddress ip=InetAddress.getByName("www.javatpoint.com");
System.out.println("Host Name: "+ip.getHostName());
System.out.println("IP Address: "+ip.getHostAddress());
}catch(Exception e){System.out.println(e);}
}
}