Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InetSocketAddress vs InetAddress

Can anyone please explain me is there is any difference between InetAddress and InetSocketAddress classes in java, and if there is a difference, please explain the same. I google-ed it up but I couldn't find anything explanatory.

like image 998
Rajat Avatar asked Feb 01 '13 14:02

Rajat


People also ask

What is InetSocketAddress?

public InetSocketAddress(int port) Creates a socket address where the IP address is the wildcard address and the port number a specified value. A valid port value is between 0 and 65535. A port number of zero will let the system pick up an ephemeral port in a bind operation.

How to Create socket address In Java?

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.

What is SocketAddress Java?

Class SocketAddress As an abstract class, it is meant to be subclassed with a specific, protocol dependent, implementation. It provides an immutable object used by sockets for binding, connecting, or as returned values.

How are instances of InetAddress class created?

The InetAddress class doesn't have public constructors, so you create a new instance by using one of its factory methods: getByName(String host): creates an InetAddress object based on the provided hostname.

What is an inetsocketaddress?

InetSocketAddress. public InetSocketAddress (int port) Creates a socket address where the IP address is the wildcard address and the port number a specified value. A valid port value is between 0 and 65535. A port number of zero will let the system pick up an ephemeral port in a bind operation. Parameters:

What is the difference between inetsocketaddress and hashCode?

The two are equal if they represent the same inetaddress and port number, or hostname and port number in case of unresolved address. 9. hashcode () : Returns the hashcode for this InetSocketAddress object.

How does inetsocketaddress compare the hostname&port number?

Two instances of InetSocketAddress represent the same address if both the InetAddresses (or hostnames if it is unresolved) and port numbers are equal. If both addresses are unresolved, then the hostname & the port number are compared.

When is the result of an argument to an inetsocketaddress true?

The result is true if and only if the argument is not null and it represents the same address as this object. Two instances of InetSocketAddress represent the same address if both the InetAddresses (or hostnames if it is unresolved) and port numbers are equal.


1 Answers

InetAddress

An instance of an InetAddress consists of an IP address and possibly its corresponding host name

InetSocketAddress

This class implements an IP Socket Address (IP address + port number) It can also be a pair (hostname + port number), in which case an attempt will be made to resolve the hostname

like image 90
Robin Chander Avatar answered Sep 30 '22 13:09

Robin Chander