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.
- 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
- 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
- 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
- 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
- getPort() : Returns the port number for this socket address.
Syntax : public final int getPort()
- getAddress() : Returns the IP address of this socket address.
Syntax : public final InetAddress getAddress()
- getHostName() : Returns the host name, using reverse lookup if it was created using an IP address.
Syntax : public final String getHostName()
- getHostString() : Returns the host name if created with hostname or string representation of the address literal used for creation.
Syntax : public final String getHostString()
- isUnresolved() : Returns a boolean value indicating whether this address is resolved or not.
Syntax : public final boolean isUnresolved()
- 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()
- 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
- hashcode() : Returns the hashode for this InetSocketAddress object.
Syntax : public final int hashCode()
Java Implementation :
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
Official Java Documentation
This article is contributed by Rishabh Mahrsee. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Networking in Java | Set 1 (Java.net.InetAddress class)
- Java.net.Inet4Address class in Java
- Java.net.Inet6Address class in Java
- Java.net.InterfaceAddress class in Java
- Java.net.JarURLConnection class in Java
- Java.net.DatagramSocket class in Java
- Java.net.DatagramPacket class in Java
- Java.net.URLEncoder class in Java
- Java.net.HttpURLConnection Class in Java
- Java.net.Authenticator class in Java
- Java.net.URLDecoder class in Java
- Java.net.NetworkInterface class in Java
- URL class in Java with Examples
- Javax.servlet.http.Cookie class in Java
- Java.net.URI class in Java
- Reading from a URL using URLConnection Class
- Java.net.HttpCookie in Java
- Datagrams in Java
- Finding IP address of a URL in Java
- Simple Calculator using TCP in Java
Improved By : Akanksha_Rai