Network Programming Section 2
Network Programming Section 2
Java provides a comprehensive set of classes and interfaces in the java.net package
for network communication.
Some key classes and interfaces such as:
• InetAddress: handle Internet addresses both as host names and as IP
addresses
• Socket: Represents an endpoint for communication between two machines
over the network.
Prepared by: Eng/ Basma Elshoky
equals() Returns true if this IP address is the same as that of the object specified.
Equals() method don’t consider hostnames while comparing and only
consider IP address associated.
getAddress() Returns the raw IP address of this InetAddress object as an array. The
order in which bytes appear in an array is the same as in IP address i.e.
getAddress[0] will contain the highest order byte.
getByAddress() Create an InetAddress object. It takes the hostname and IP address as its
parameter. The hostname can be the machine name as in
“www.geeksforgeeks.org” or its textual IP address.
getByName() Returns the IP Address of the host specified. If the host is a literal IP
address, then only its validity is checked.
getHostName() Returns the hostname for this IP Address. If this object was created with
a hostname then it is returned, otherwise, a reverse lookup is performed
to return the system configured hostname.
getCanonicalHostName() Returns the fully qualified domain name for this object. If this object was
created with a hostname then it is returned, otherwise, a reverse lookup is
performed to return the system configured hostname.
isReachable() Returns true if this address is reachable. ICMP echo requests are used if
permission can be granted otherwise the host tries to make a TCP
connection at port 7 of the destination. This method is used generally as a
pre-condition in various programs, to avoid Host Unreachable exceptions
in the future
isReachable() Specify the network interface to be used while checking for reachability
and the ttl parameter specifies the number of hops the echo packet makes
before exiting the network.
toString() Converts the IP address to the string. It returns the result as hostname / IP
address.
Prepared by: Eng/ Basma Elshoky
***Next Section will discuss socket class and it is method with example.
Task:
Get the IP of your university “BTU website” then apply10 methods from the table.