Open In App

Java.net.InetSocketAddress class in Java

Last Updated : 05 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

This class implements IP socket address( combination of IP address and port number). The objects of this class are immutable and can be used for binding, connecting purposes. 

Constructors : 

1. InetSocketAddress(InetAddress addr, int port) : This constructor is similar to the general structure of a socket address with the attributes for Inet address and port number. 

Syntax :public InetSocketAddress(InetAddress addr,
                 int port)
Parameters :
addr : IP address
port : port number

2. InetSocketAddress(int port) : Creates a socketaddress object with the specified port number and a wildcard IP address. A wildcard IP address has the value 0.0.0.0 and it binds your socket to all network cards. 

Syntax : public InetSocketAddress(int port)
Parameters :
port : port number

3. InetSocketAddress(String hostname, int port) : Creates a socketaddress object and binds it to specified port and host. Resolution of hostname is performed to find the IP address and that is used for binding purpose, not the host name. If the resolution returns null, the address will be flagged as unresolved. 

Syntax : public InetSocketAddress(String hostname,
                 int port)
Parameters :
hostname :  host name
port : port number

Methods : 

1. createUnresolved() : Creates a socket address with the given host and port number where no attempt is made to resolve the host name and the address is marked as unresolved. 

Syntax :public static InetSocketAddress createUnresolved(String host,
                                 int port)
Parameters :
host : host name
port : port number

2. getPort() : Returns the port number for this socket address. 

Syntax : public final int getPort()

3. getAddress() : Returns the IP address of this socket address. 

Syntax : public final InetAddress getAddress()

4. getHostName() : Returns the host name, using reverse lookup if it was created using an IP address. 

Syntax : public final String getHostName()

5. getHostString() : Returns the host name if created with hostname or string representation of the address literal used for creation. 

Syntax : public final String getHostString()

6. isUnresolved() : Returns a boolean value indicating whether this address is resolved or not. 

Syntax : public final boolean isUnresolved()

7. toString() : Returns the string representation of this InetSocket address object. First the toString() method is called on the InetAddress part and then port number is appended after a colon. 

Syntax : public String toString()

8. equals() : compares if this socketaddress object is equal to specified object. The two are equal if they represent the same inetaddress and port number, or hostname and port number in case of unresolved address. 

Syntax : public final boolean equals(Object obj)
Parameters :
obj : object to compare with

9. hashcode() : Returns the hashcode for this InetSocketAddress object. 

Syntax : public final int hashCode()

Java Implementation :  

Java




// Java program to illustrate various
// InetSocketAddress class
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
 
public class InetsockAddress
{
 
    public static void main(String[] args) throws UnknownHostException
    {
 
        // Following constructor can be used to create InetSocketAddress
        // objects.
        InetSocketAddress isa1 = new InetSocketAddress(5500);
        InetSocketAddress isa2 = new InetSocketAddress("localhost", 5050);
        InetAddress ip = InetAddress.getByName("localhost");
        InetSocketAddress isa3 = new InetSocketAddress(ip, 8800);
 
        // createUnresolved() does not attempt to resolve the hostname.
        InetSocketAddress isa4 = InetSocketAddress.createUnresolved("abc", 5055);
 
        // These InetSocketAddress objects can be used to create sockets
        // in socket programming, in place of specifying individually the IP
        // address and port number. Please refer to TCP articles for their
        // further use.
 
        // These can also be used to retrieve information about the
        // socketAddress objects.
 
        // getHostName() method
        System.out.println("Hostname : " + isa1.getHostName());
 
        // getHostString() method
        System.out.println("Host string : " + isa1.getHostString());
 
        // getAddress() method
        System.out.println("Inet address : " + isa1.getAddress());
 
        // getPort() method
        System.out.println("Port : " + isa1.getPort());
 
        // isUnresolved() method
        System.out.println("isUnresolved : " + isa1.isUnresolved());
 
        // equals() method
        System.out.println("isa1==isa2 : " + isa1.equals(isa2));
 
        // toString() method
        System.out.println("toString : " + isa1.toString());
 
        // hashCode() method
        System.out.println("hashCode : " + isa1.hashCode());
    }
 
}


Output : 

Hostname : 0.0.0.0
Host string : 0.0.0.0
Inet address : 0.0.0.0/0.0.0.0
Port : 5500
isUnresolved : false
isa1==isa2 : false
toString : 0.0.0.0/0.0.0.0:5500
hashCode : 5500

References : 
Official Java Documentation 

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads