Advanced JAVA 5
Java Networking
Java Networking is a concept of connecting two or more computing devices together so that we can share resources.
Java socket programming provides facility to share data between different computing devices.
Advantage of Java Networking
1. sharing resources.
2. centralize software management.
java.net package encapsulate large number of classes and interface that provides an easy-to use means to access network resources. Here are some important classes and interfaces of java.net package.
Some Important Classese
CacheRequest, CookieHandler,
CookieManager, Datagrampacket,
Inet Address, ServerSocket,
Socket, DatagramSocket,
Proxy, URL,
URLConnection
Some Important Interfaces
CookiePolicy, CookieStore,
FileNameMap, SocketOption,
InetAddress, ServerSocket,
SocketImplFactory, ProtocolFamily
Java InetAddress class
Inet Address encapsulates both numerical IP address and the domain name for that address. Inet address can handle both IPv4 and IPv6 addresses. Inet Address class has no visible constructor. To create an Inet Address object, you have to use Factory methods.
Method | Description |
public static InetAddress getLocalHost() throws UnknownHostException | it returns the instance of InetAdddress containing local host name and address. |
public static InetAddress getByName(String hostname) throws UnknownHostException | it returns the instance of InetAddress containing LocalHost IP and name. |
public static InetAddress[ ] getAllByName(String hostname) throws UnknownHostException | it returns the instance of InetAddress containing all LocalHost IP and name. |
static String format(Locale l, String format, Object... args) | returns formatted string with given localer. |
public String getHostName() | it returns the host name of the IP address. |
public String getHostAddress() | it returns the IP address in string format. |
Example using InetAddress class
import java.net.*;
class Test
{
public static void main(String[] args)
{
InetAddress address = InetAddress.getLocalHost();
System.out.println(address);
address = InetAddress.getByName("www.bethedeveloper.com");
System.out.println(address);
InetAddress sw[] = InetAddress.getAllByName("www.google.com");
for(int i=0; i< sw.length; i++)
{
System.out.println(sw[i]);
}
}
}
Output:
Welcome-PC/59.161.87.227
www.bethedeveloper.com/208.91.198.55
www.google.com/74.125.236.115
www.google.com/74.125.236.116
www.google.com/74.125.236.112
www.google.com/74.125.236.113
www.google.com/74.125.236.114
www.google.com/2404:6800:4009:802:0:0:0:1014
Dinesh Kumar S is a 23-year-old System Administrator who enjoys playing games, listening to music and learning new technology. He is friendly and generous, but can also be very lazy and crazy.
Share this
Learn-JAVA