lecture 2 - internet standards and protocols(1)
DESCRIPTION
Internet Standards and ProtocolsTRANSCRIPT
Lecture 2 – Internet Standards and Protocols
TNE70002 Network Computing
Objectives
TO INTRODUCE:
• IP addresses and Domain Name System (DNS)
• InetAddress class and associated methods
• Uniform Resource Identifier (URI), Uniform Resource Name (URN), Uniform Resource Locator (URL)
• Java URL class and associated methods
IP Addresses-IPv4
• Computer locations on a IP network (including the internet) are given by an IP address
• In IPv4, addresses are 32-bit numbers There are 232 unique IP addresses in this
representation (~4 billion) which is less than the population in the world (~6.5 billion)
Written as 4 dot-separated 8-bit segments (quads) Example the IP address for www.swin.edu.au is 136.186.1.10
Continued
IP Addresses-IPv6
• In IPv6, addresses are 128-bit numbers We can now represent 2128 unique IP addresses Consists of 8 colon-separated 16-bit segments usually written as
8 blocks of 4 hexadecimal colon-separated digits Example the address for www.ipv6.com.cn is 2001:0250:02FF:0210:025:8BFF:FEDE:67C8
Leading zeros do not need to be written and a double colon (::) one of which may appear in any address could replace multiple zero blocks. Example FEDC:0000:0000:0000:00DC:0000:7076:0010 could be written as FEDC::DC:0:7076:10
In mixed networks of IPv6 and IPv4 the last four bytes of the IPv6 address can be written as an IPv4 dotted quad address. Example FEDC:BA98:7654:3210:FEDC:BA98:7654:3210 could be written as FEDC:BA98:7654:3210:FEDC:BA98:118.84.50.16
Domain Naming System
• In addition to an IP address, computers can have an easy-to-remember domain name Example www.swin.edu.au
• Domain Naming System (DNS) translates from domain name to IP address
• When an application requests data from a domain name It asks the DNS for the numeric IP Address It includes the numeric address with the request for
data
Java Class InetAddress (Ex. 1 & 2)
• The Java class InetAddress represents a network address. You can use it (or its method called getByName) to convert both ways between IP and domain name forms
InetAddress address1 = InetAddress.getByName(“www.swin.edu.au”);
address1.getHostAddress(); //returns IP “136.186.1.10”
InetAddress address2 = InetAddress.getByName(“136.186.1.10”);
address2.getHostName(); //returns DNS name “www.swin.edu.au”
Java Class InetAddress (Ex. 3)
• The Java class InetAddress also has a static method getAllByName which can be used to perform one-to-many conversion (both ways) between IP address and domain name
InetAddress [] addresses = InetAddress.getAllByName(“www.swin.edu.au”);
Addresses[0].getHostAddress(); //returns IP “136.186.1.10”
InetAddress[ ] addresses = InetAddress.getAllByName(“136.186.1.10”);
Address[0].getHostName(); //returns name “www.swin.edu.au”
Java Class InetAddress (Ex. 4)
• In applications we create it is often desirable to retrieve the IP address and/or the name of the current machine
InetAddress address = InetAddress.getLocalHost();
address.getHostAddress(); //returns IP address as a string
address.getHostName(); //returns DNS name
Alternatively we can just use the println method to print the entire address object via it’s toString() method
System.out.println(address); //displays something like swin.edu.au/ 136.186.1.10
URI, URL and URN• Uniform Resource Identifier (URI) is a structured string of
characters used to identify a name or a resource on the internet. Such identification enables interaction with representations of resources over a network using specific protocols
• A URI can be classified as a Uniform Resource Name (URN) which names a resource on a network (eg. A person’s name). The resource may be a file, program, streaming media, or any other resource
• A URI can also be classified as a Uniform Resource Locator (URL) which locates a resource on a network. (eg. A person’s address)
• ISBN 0486275574 cites unambiguously a specific edition of Shakespeare's play Romeo and Juliet , its URN. However, to find and read it one needs it’s location given by the URL: http://shakespeare.mit.edu/romeo_juliet/hakespear
URL Parts (1)
• URLs are composed of up to five parts: Scheme (protocol) Authority
• User Info (eg. username, password)• Host (most common)• Port
Path Query String Fragment (Ref or Section)
scheme://user@hostname:port/path/path?query#fragmentftp://ben:[email protected]/
https://www.motorcars.com.au:8080/viewContract?contractId=3456&sessionId=s180587668
http://en.wikipedia.org/wiki/URL#Example:_HTTP_URLs scheme
user info hostname
port
path
query string
fragment
URL Parts (2)scheme://user@hostname:port/path/path?query#fragment
• Schemes (protocols) identify the way the resource can be accessed & retrieved; eg. http, ftp, rmi
• Authoriy User info may include colon separated username and password credential of the client Host name identifies the host on which the resource resides Port is sometimes optional because most well known schemes have a default port, eg. http
80
• Path is a series of directory (folder) names, ending in a filename. The path doesn’t need to represent a real file; it might be an object or operation name
• Query strings are heavily used in dynamic web systems to encode variables sent from the client, using the format parameter1=value1¶meter2=value2
• Fragments are pointers to a part of a document or a file
URLs are not unique identifiers
• URLs locate one resource or item on the internet, but they do not uniquely identify it; different URLs may point to the same underlying resource
• Two different hostnames may resolve to same web server: http://www.swin.edu.au/ http://www.swinburne.edu.au/
• Two paths may resolve to same file: http://www.swin.edu.au/staff/index.html http:// www.swin.edu.au/contact/../staff/index.html
• An RMI object registered/advertised under two names: rmi://localhost/Obj1 [Naming.rebind(“Obj1”, server)] rmi://localhost/Obj2 [Naming.rebind(“Obj2”, server)]
The Java Class URL
• java.net.URL class is an abstraction of a Uniform Resource Locator such as http://www.swin.edu.au or ftp://ftp.mine.org/pub
• It extends java.lang.Object and implements the java.io.Serializable interface and is a final class that cannot be subclassed thus:
public final class URL extends Object implements Serializable
• The URL class is the simplest way for a Java program to locate and retrieve data from network
• No need to worry about the details of protocol being used, format of data being retrieved or how to communicate with the server
• You simply tell Java the URL and it gets the data for you
• Standard Java can only handle a limited number of protocols and content types, however, this capability can be extended to handle more protocols and types of data
The Java Class URL
• Think of URLs as objects (rather than just simple strings) with fields (protocol, hostname, port, path, query string and fragment) and methods
• Fields are set using the URL constructors
• URL objects are immutable so field values (once set) can’t be changed, hence they are thread-safe
• Field values can be retrieved using their respective get methods (getProtocol(), getHost(), getPort() etc.)
• There are six constructors that can be used differing in information required though they all throw MalformedURLException if you try to form a URL with an unsupported protocol and may throw the same exception if the URL is syntactically incorrect
Creating a URL object
• The simplest URL constructor just takes one string argument (the absolute URL) and creates a URL object
public URL(String url) throws MalformedURLException
• Thus we can use this constructor to create a URL object as follows:
…
try {
URL u = new URL("http://www.swin.edu.au/");
}
catch (MalformedURLException ex) {
System.err.println(ex);
}
…
Testing for supported protocols (Ex. 5)
• Which protocols does a particular virtual machine support?
…
import java.net.*;
public class ProtocolTester {
public static void main(String[] args) {
// hypertext transfer protocol
testProtocol("http://www.adc.org");
// secure http
testProtocol("https://www.amazon.com/exec/obidos/order2/");
// file transfer protocol
testProtocol("ftp://metalab.unc.edu/pub/languages/java/javafaq/");
// Simple Mail Transfer Protocol
testProtocol("mailto:[email protected]");
testProtocol("file://metalab.unc.edu/pub/myfile.pdf");
…
protocol tester (cont.)
• And the testProtocol method
…
private static void testProtocol(String url) {
try {
URL u = new URL(url);
System.out.println(u.getProtocol( ) + " is supported");
}
catch (MalformedURLException ex) {
String protocol = url.substring(0, url.indexOf(':'));
System.out.println(protocol + " is not supported");
}
}
}
…
protocol tester (cont.)
• When we compile and run ProtocolTester the result might look something like the following (the exact output will depend on the virtual machine being used)
…
% java ProtocolTester
http is supported
https is supported
ftp is supported
mailto is not supported
file is supported
…
Splitting a URL to its parts (Ex. 6)
• Using the appropriate methods of the URL class it is possible to split a URL object to its constituent parts
• The methods to use include: getProtocol(), getFile(), getHost, getPort(), getPath(), getRef(), getAuthority(), getUserInfo(), getQuery()
import java.net.*;
public class URLSplitter {
public static void main(String args[]) {
for (int i = 0; i < args.length; i++) {
try {
URL u = new URL(args[i]);
System.out.println("The URL is " + u);
System.out.println("The scheme is " + u.getProtocol( ));
System.out.println("The user info is " + u.getUserInfo( ))
…
Retrieving data from a URL
• Playing with URLs may be an interesting exercise but what is usually more exciting is the data they actually point to
• The URL class has several methods designed to retrieve data from a location pointed to by a URL
public InputStream openStream( ) throws IOException
public URLConnection openConnection( ) throws IOException
public URLConnection openConnection(Proxy proxy) throws IOException
public Object getContent( ) throws IOException
public Object getContent(Class[] classes) throws IOException
• These methods return the data found at the URL as an instance of different classes
Using the openStream method to retrieve data from a URL (Ex. 7)
• This method connects to the resource refrenced by the URL performs necessary handshaking between client and server and returns an InputStream object from which data can be read
• Data received is raw: ASCII if you are reading an ASCII text file, HTML if you are reading an HTML file and binary image data if you are reading an image file an so on
…
try {
URL u = new URL("http://www.swin.edu.au");
InputStream in = u.openStream( );
Scanner scan = new Scanner(in);
while (scan.hasNextLine() {
System.out.println(scan.nextLine());
}
catch (MalformedURLException me) {
System.err.println(…);
}
catch (IOException io) {
System.err.println(…);
}
…
Packages and classes used
• java.net InetAddress UnknownHostException URL MalformedURLException
• java.io InputStream IOException
• java.util Scanner