Objectives
TO INTRODUCE:
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
//returns IP 136.186.1.10
//returns IP 136.186.1.10
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
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
try {
URL u = new URL("http://www.swin.edu.au/");
}
catch (MalformedURLException ex) {
System.err.println(ex);
}
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:elharo@metalab.unc.edu");
testProtocol("file://metalab.unc.edu/pub/myfile.pdf");
% java ProtocolTester
http is supported
https is supported
ftp is supported
mailto is not supported
file is supported
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();
}
InetAddress
UnknownHostException
URL
MalformedURLException
java.io
InputStream
IOException
java.util
Scanner