Java.net.InetSocketAddress class in Java
Last Updated :
05 Oct, 2021
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
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
public class InetsockAddress
{
public static void main(String[] args) throws UnknownHostException
{
InetSocketAddress isa1 = new InetSocketAddress( 5500 );
InetSocketAddress isa2 = new InetSocketAddress( "localhost" , 5050 );
InetAddress ip = InetAddress.getByName( "localhost" );
InetSocketAddress isa3 = new InetSocketAddress(ip, 8800 );
InetSocketAddress isa4 = InetSocketAddress.createUnresolved( "abc" , 5055 );
System.out.println( "Hostname : " + isa1.getHostName());
System.out.println( "Host string : " + isa1.getHostString());
System.out.println( "Inet address : " + isa1.getAddress());
System.out.println( "Port : " + isa1.getPort());
System.out.println( "isUnresolved : " + isa1.isUnresolved());
System.out.println( "isa1==isa2 : " + isa1.equals(isa2));
System.out.println( "toString : " + isa1.toString());
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
Similar Reads
Java.net.Inet4Address class in Java
This class extends the InetAddress class and represents an IPv4 address. It provides methods to interpret and display useful information about IP addresses. Methods of this class take input in 4 formats: d.d.d.d: When this format is used as input, each of the given values are assigned to 4 bytes of
3 min read
Java.net.Inet6Address class in Java
This class represents IPv6 address and extends the InetAddress class. Methods of this class provide facility to represent and interpret IPv6 addresses. Methods of this class takes input in the following formats: x:x:x:x:x:x:x:x -This is the general form of IPv6 address where each x can be replaced w
5 min read
Java.net.InterfaceAddress class in Java
This class represents a network interface address. Every device that has an IP address has an IP address on the network interface. In fact the ping command doesn't ping a device but the devices interface address. Java provides certain methods to deal with interface addresses which can be used in pla
2 min read
Java.net.DatagramSocket class in Java
Datagram socket is a type of network socket which provides connection-less point for sending and receiving packets. Every packet sent from a datagram socket is individually routed and delivered. It can also be used for sending and receiving broadcast messages. Datagram Sockets is the java's mechanis
9 min read
Java.net.DatagramPacket class in Java
This class provides facility for connection less transfer of messages from one system to another. Each message is routed only on the basis of information contained within the packet and it may be possible for different packets to route differently. There is also no guarantee as to whether the messag
5 min read
Java.net.NetworkInterface class in Java
This class represents network interface, both software as well as hardware, its name, list of IP addresses assigned to it, and all related information. It can be used in cases when we want to specifically use a particular interface for transmitting our packet on a system with multiple NICs. What is
6 min read
Introducing Threads in Socket Programming in Java
Prerequisites : Socket Programming in Java This article assumes that you have basic knowledge of socket programming in java and the basic details of client-server model used in communication. Why to use threads in network programming? The reason is simple, we don't want only a single client to conne
7 min read
Explicitly assigning port number to client in Socket
Prerequisite: Socket programming in C/C++.In socket programming, when server and client are connected then the client is provided any random port number by an operating system to run and generally, we don't care about it, But in some cases, there may be a firewall on the client-side that only allows
4 min read
Provider entrySet() method in Java with Examples
The entrySet() method of java.security.Provider class is used to return an unmodifiable Set view of the property entries contained in this Provider. Syntax: public Set<Map.Entry> entrySet() Return Value: This method returns a set view of the mappings contained in this map Below are the example
3 min read
Network Scanner in Python
A network scanner is one major tool for analyzing the hosts that are available on the network. A network scanner is an IP scanner that is used for scanning the networks that are connected to several computers. To get the list of the available hosts on a network, there are two basic methods - ICMP Ec
3 min read