Professional Documents
Culture Documents
Network Programming 1
Internet Address
Devices connected to the Internet are called nodes.
Nodes that are computers are called hosts.
Each node or host is identified by at least one unique
number called an Internet address .
IPv4 addresses are 4-byte-long
IPv6 addresses are 16-byte-long.
An IPv4 address is written as four unsigned bytes, each
ranging from 0 to 255, with the most significant byte first.
An IPv6 address is normally written as eight blocks of four
hexadecimal digits separated by colons.
Network Programming 2
Internet Address
Every computer connected to the Internet should have
access to a machine called a domain name server.
Most domain name servers only know the addresses of
the hosts on their local network, plus the addresses of a
few domain name servers at other sites.
If a client asks for the address of a machine outside the
local domain, the local domain name server asks a
domain name server at the remote location and relays the
answer to the requester.
Network Programming 3
InetAddress Class
InetAddress Class
Network Programming 5
InetAddress Class
• Represents IP address of a node
• No public constructor
• Methods include:
getByName
getAllByName
getLocalHost
getByAddress
getByName
• Returns InetAddress object for the host whose
hostname is specified in the argument
return object
Else
throw UnknownHostException
getByName - hostname
EXAMPLE:
import java.net.*;
public class OReillyByName{
public static void main (String[] args){
try {
InetAddress addr=InetAddress.getByName("www.oreilly.com");
System.out.println(addr);
}
catch (UnknownHostException ex) {
System.out.println("Could not find www.oreilly.com");
}
}
}
OUTPUT:
www.oreilly.com/208.201.239.36
getByName – IP address
• Doesn’t lookup DNS
• Lookup DNS only if implicitly(toString) /
explicitly(getHostName) specified to.
• If (host found) then
Return object
• Else
hostname = ipaddress
Throw no exception
getByName – IP address
EXAMPLE
import java.net.*;
public class OReillyByAddress{
public static void main (String[] args) {
try {
InetAddress address =
InetAddress.getByName("208.201.239.37");
System.out.println(address);
}
catch (UnknownHostException ex) {
System.out.println("Could not find 208.201.239.37");
}
}
}
OUTPUT
www.oreilly.com/208.201.239.37
getAllByName
• Returns all IP address for the given hostname
• If (host found)
Return object containing all IP address
• Else
Throw UnKnownHostException
• System properties
Networkaddress.cache.ttl
No. of seconds, a successful DNS result will be in cache
Networkaddress.cache.negative.ttl
No. of seconds, a unsuccessful DNS result will be in cache
Security
1. Untrusted code cannot lookup for arbitrary 3 rd
party host. Can only lookup for:
Codebase
Localhost
Only 127.0.0.1 (not actual hostname & IP address)
2. Test whether a host can be resolved
public void checkConnect(String hostname, int
port)
Port = (-1 / positive integer)
2. GETTER METHODS
• InetAddress class -> 3 methods
• Return hostname as string & IP address as
both string and byte array.
• Retrieve information from InetAddressObject
• Getter methods
A)getHostName
B) getAddress
C) getHostAddress
A) getHostName
• If (host_name available)
Return (String)host_name
• Else
Return (String)(host_name = IP address)
a. equals
b. Hashcode
c. toString
equals
• If ((InetAddress Object) && (IP addr are
equal))
Return TRUE
• Else
Return FALSE
import java.net.*;
public class IBiblioAliases {
equals
public static void main (String args[]) {
try {
InetAddress ibiblio = InetAddress.getByName("www.ibiblio.org");
InetAddress helios =
InetAddress.getByName("helios.metalab.unc.edu");
if (ibiblio.equals(helios)) {
System.out.println("www.ibiblio.org is the same as
helios.metalab.unc.edu");
}
else {
System.out.println("www.ibiblio.org is not the same as
helios.metalab.unc.edu");
}
}
catch (UnknownHostException ex) {
System.out.println("Host lookup failed.");
}}}
RESULT www.ibiblio.org is the same as helios.metalab.unc.edu
hashCode
• Returns hash value of the object
• EXAMPLE
InetAddress thisComputer = InetAddress.getLocalHost();
String address = thisComputer.toString();
• RESULT
hostname/dotted quad address
THE NETWORKINTERFACE
CLASS
NetworkInterface Class
• Represents the network interface
• Methods include:
1. Factory
2. Getter
3. Object
1. FACTORY METHODS
• Used for creating & initializing the objects
• 3 methods
getByName
getByInetAddress
getNetworkInterfaces
getByName
• Retrieves NetworkInterface object based on
interface name
try {
NetworkInterface ni = NetworkInterface.getByName("eth0");
if (ni == null) {
System.err.println("No such interface: eth0" );
}
}
catch (SocketException ex) {
System.err.println("Could not list sockets." );
}
getByInetAddress
• Retrieves NetworkInterface object based on IP address
try {
InetAddress local = InetAddress.getByName("127.0.0.1");
NetworkInterface ni = NetworkInterface.getByInetAddress(local);
if (ni == null) {
System.err.println("That's weird. No local loopback address.");
}
}
catch (SocketException ex) {
System.err.println("Could not list sockets." );
}
catch (UnknownHostException ex) {
System.err.println("That's weird. No local loopback address.");
}
getNetworkInterfaces
• Retrieves NetworkInterface object for all interfaces
import java.net.*;
import java.util.*;
public class InterfaceLister {
public static void main(String[] args) throws Exception {
Enumeration interfaces = NetworkInterface.getNetworkInterfaces( );
while (interfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) interfaces.nextElement( );
System.out.println(ni);
}
}
}
getNetworkInterfaces
OUTPUT