java.net package contains classes and interfaces that provides networking support to the java application commonly used classes and interface of this package are :
InetAddress
Socket
ServerSocket
DatagramSocket
DatagramPacket
InetAddress This class provides object representation of public contructor rather it provides Factory method for creating its object.
Factory is a creational design pattern that is used to control creation of object. Imiplementation of this design pattern is proivded whti the help of factory class.
A Factory class is a class that contains factory method. A factory method is a method that creates and returns objects.
Singleton Class It is a class that can be instantiated only once.
Class A
{
private static A a = null;
private A()
{
-------------------
-------------------
-------------------
}
public satif A getA()
{
if(a==null)
{
a = new A();
}
return a;
}
}
A x = A.getA();
A x1 = A.getA();
Various Factory Method of InetAddress class are :
getLocalHost(); Returns an InetAddress object representing the IP Address of the current machine.
syntax:
public statif InetAddress getLocalHost()throws UnknownHostException;
Example: InetAddress add = InetAddress.getLocalHost();
getByName(); Returns an InetAddress object representing IP Address of the given machine.
syntax:
public static InetAddress getByName(String HostName)throws UnknownHostException;
getAllByName(); Returns an array of InetAddress object each element of an array represents an InetAddress of the given host.
syntax:
public static InetAddress[] getAllByName(String HostName)throws UnknownHostException;
Non-Factory Method of InetAddress Class
getHostAddress(); Returns IP Address as string public String getHostAddress();
syntax:
public String getHostAddress();
getHostName(); Returns HostName as a String.
syntax:
public String getHostName();
Example of : Find IP Address of Machine
0 Comments:
Post a Comment