230 likes | 321 Views
Internet Addressing. Introduction. The Internet is a vast collection of machines and devices spread out across the world. There is an important need to identify and locate a specific one. We will look into greater detail about how addresses work and how they are represented in Java.
E N D
Introduction • The Internet is a vast collection of machines and devices spread out across the world. • There is an important need to identify and locate a specific one. • We will look into greater detail about how addresses work and how they are represented in Java.
Local Area Network Addresses • Devices connected to a LAN have their own unique hardware address. • This address is useful only in the context of a LAN. In other words, it cannot be used to locate machines on the Internet. • This address cannot be used to indicate the location of a machine as it is possible to move the machine somewhere else (e.g. notebook computers).
Java network programmers need not be concerned with details about how data is routed within a LAN. In fact, Java does not provide access to data link protocols (which are network dependent) used by LANs. • No matter what type of LAN is used, software can be written for it provided it supports TCP/IP (which is network independent).
Internet Protocol Addresses • Devices having a direct Internet connection are allocated a unique IP address. • IP addresses may be • Static • Bound permanently to a certain machine/device • Dynamic • Leased to a particular machine/device for a certain period of time.
The IP address is used by the Internet Protocol to route IP datagrams to the correct location.
Structure of the IP Address • Under Internet Protocol version 4 (IPv4), the IP address is a 32-bit number made up of 4 octets. E.g: 192.168.1.1 • There are 5 classes of IP addresses: • Class A 0.0.0.0 to 127.255.255.255 • Class B 128.0.0.0 to 191.255.255.255 • Class C 192.0.0.0 to 223.255.255.255 • Class D 224.0.0.0 to 239.255.255.255 • Class E 240.0.0.0 to 247.255.255.255
0 1 2 3 4 5 6 7 8 16 24 32 A Network ID Host ID 0 B Host ID Network ID 1 0 C Network ID Host ID 1 1 0 D Multicast Group ID 1 1 1 0 E Reserved for Future Use 1 1 1 1 0
Each private network is allocated a network ID that is a unique identifier for a specific network. The network administrator is responsible for assigning host IDs to machines in the network. • The use of the classification scheme can help to reduce wastage of addresses.
Obtaining an IP Address • The Internet Corporation for Assigned Names and Numbers (ICANN) is responsible for allocating blocks of IP addresses. • A person setting up a private network would be allocated either a Class A, B or C address, and could then assign host IP addresses to the machines on that particular network.
Special IP Addresses • A loopback or localhost address (127.0.0.1) refers to the local machine. • It is useful for programmers to connect to the local machine for testing their network software. The connection using this address is possible whether or not there is a connection to the Internet.
Another set of useful IP addresses are those reserved for private networking. • These addresses can be used safely within a private network as they are not exposed to the public Internet. • Class A 10.0.0.0 to 10.255.255.255 • Class B 172.16.0.0 to 172.31.255.255 • Class C 192.168.0.0 to 192.168.255.255 • On the Internet, routers will not forward data for these addresses.
The Domain Name System • The domain name system (DNS) allows textual names to be associated with IP addresses. • Any entity, be it commercial, government or private, can apply for a domain name. The name can be used by people to locate that entity on the Internet.
In addition, organizations can allocated their own hostnames once they have their domain name. Examples: ftp.davidreilly.com and www.davidreilly.com.
How Does the DNS Work? • There are too many domain-name-to-IP-address mappings to handle on one system. • The DNS is a distributed database in which responsibility for accepting new registrations, and returning the addresses of existing registrations, is spread out across many different hosts.
The distribution of such responsibilities forms an hierarchical structure such as shown below: .net .com .gov .edu .mil .au .uk davidreilly awl .co .org
Domain Name Resolution • When a request for a hostname is made (e.g: www.aol.com), the operating system of the client computer contacts the local DNS server on the LAN. • The local DNS server needs to locate the domain server "aol.com". To do this, it queries the "root" level domain server, which refers it to the ".com". • When "aol.com" has been located, the local DNS server then queries it for the IP address of "www.aol.com"
get .com server ROOT DNS server LAN DNS server get aol server www.aol.com .com DNS server
DNS requests can be cached on the LAN DNS server. This prevents overloading the root-level domains. • This also improves network performance as the delay between requesting a domain name and receiving a response is diminished.
Internet Addressing with Java • In Java, IP addresses (whether in dotted decimal format or as a hostname) are represented as java.net.InetAddress objects. • Refer textbook (pg 63) for a description of some of the methods in the class InetAddress. http://java.sun.com/j2se/1.4.2/docs/api/java/net/InetAddress.html
DEMO` • Example 1: • A program to find out the IP address of the current machine • Example 2: • A program which resolves hostnames to IP addresses and then attempts to perform a reverse lookup of the IP address. DEMO RUN
import java.net.*; class LocalHostDemo { public static void main(String args[]) { try { InetAddress localAddr = InetAddress.getLocalHost(); System.out.print("IP address: "); System.out.println(localAddr.getHostAddress()); System.out.println(localAddr.getHostName()); } catch (UnknownHostException uhe) { System.out.println("Unable to resolve localhost"); } } }
import java.net.*; class NetworkResolverDemo { public static void main(String args[]) { if (args.length != 1) System.out.println("Syntax: NetworkResolverDemo host"); try { InetAddress addr = InetAddress.getByName(args[0]); System.out.print("IP address: "); System.out.println(addr.getHostAddress()); System.out.print("Hostname: "); System.out.println(addr.getHostName()); } catch (UnknownHostException uhe) { System.out.println("Unable to resolve hostname"); } } }