Week 6: Basic Java Networking
Overview of Networking
- Relatively easy in Java (compare with C, C++, etc)
- URL - Uniform Resource Locator, the address of resource
in the Internet
- Use <APPLET> in the browser, browser did the
undercover networking for you
- Network classes in java.net package
- Remember the JDK doc is at http://www.javasoft.com/products/jdk/1.1/docs/
Networking Basics
Application
(HTTP, ftp, telnet,etc) |
Transport
(TCP, UDP, etc) |
Network
(IP, etc) |
Link
(Device driver) |
- Use classes in java.net to provide system-independent
network communication
- Java can use TCP or UDP
- TCP (Transport Control Protocol) is a connection-based
protocol that provides a reliable flow of data between
two computers
- Hypertext Transfer Protocol (HTTP), File Transfer
Protocol (FTP), and Telnet are all examples of
applications that use TCP
- UDP (User Datagram Protocol) is a protocol that sends
independent packets of data called datagrams, from one
computer to another with no guarantees about arrival (it
is not connection-based)
E.g. Clock server, ping command, etc
Ports
- The use of ports let computer which application to
forward the data to
- 32-bit IP address, 16-bit port number
- TCP, UDP both use ports
- In TCP, an application extablishes a connection by
binding a socket to a port number
(socket is like a phone, and port is part of the number)
- Binding registers the application with the system to
receive all data destined for that port
- Port numbers ranges from 0 to 65535
- 0-1023 are restricted as well known ports
(e.g. 80 for http, 21 for ftp, 23 for telnet, 7 for echo)
- In Java, the URL, URLConnection, Socket, and SocketServer
classes all use TCP
- The DatagramPacket and DatagramServer classes use UDP
Working with URLs
- URL is a reference (an address) to a resource on the
Internet. URLs are the doorway to Internet and the World
Wide Web
- In Java, we have URL class from java.net. Once created,
we can use its methods to find out its host name,
filename, and other information
- All URL has two components:
The protocol identifier
The resource name
- For example: http://www.openloop.com
http is the protocol (Hypertext Transfer Protocol)
We can also have (File Transfer Protocol) ftp, gopher,
File and News
- The resouce name contains one or more of the following
components:
- Host name - the name of the machine the resource
lives on
- Filename - the pathname to the file on the
machine
- Port number - the port number connected to
(Optional)
- Reference - A reference to a named anchor within
a resource (Optional)
- For example:
http://www.openloop.com:80/index.html#bottom
Creating a URL
- Can be created from String
- Human string will be http://www.openloop.com
In Java:
URL myObj = new URL("http://www.openloop.com/");
// the / at the end has a default of /index.html
- This is an absolute URL - contains all of the information
necessary to reach the resource in question
- A relative URL contains only enough information to reach
the resource relative to (or in the context of) another
URL
In HTML, if A.html, B.html and C.html is in the same directory on the server. A.html can refer to the others as follows:
<A HREF="B.html">This is a title for B</A>
<A HREF="C.html">This is a title for C</A>
In Java, we can create relative URL object as well
URL myObj = new URL("http://www.openloop.com/");
URL otherObj = new URL(myObj, "index2.html"); // index2.html is on site openloop
- Two constructor of the URL class:
URL(String) // Absolute URL object
URL(URL, String) // relatvie URL object
- The general forms are:
URL(String absoluteURL)
URL(URL baseURL, String relativeURL)
Other URL Constructors
- These constructor are useful when working with hostname,
filename, port number and references
- URL myObj = new URL("http",
"www.openloop.com", "/index.htm");
- The above is equivalent to
URL("http://www.openloop.com/index.htm");
- The final constructor will take a port as well:
URL myObj = new URL("http",
"www.openloop.com", 80,
"/index.htm");
MalformedURLException
Each of the four URl constructors will throws a
MailformedURLException if there is a null argument or unknown
protocol
// Catch and handle this exception by embedding your URL constructor
try
{
URL myURL = new URL(...)
}
catch
{
...
// exception handler code here
...
} // We will talk about exception handling in detail later on
Parsing a URL
- URL class methods:
- getProtocol()
- getHost()
- getPort()
- getFile() - Returns the filename component of the
URL
- getRef - Returns the reference component of the
URL
- Yes, the URL class is somewhat HTTP centric
Example:
import java.net.*
import java.io.*
try
{
aURL = new URL("http://java.sun.com/");
System.out.println("protocol = " + aURL.getProtocol());
}
catch (MalformedURLException e)
{
System.out.println("MalformedURLException:" + e);
}
Reading Directly from a URL
- Use the openStream method to get a stream from the
content of the URL
- openStream returns a java.io.InputStream
Example:
...
// This part of the java application reads the content from the input stream
// and then echo out the contents to the display
try
{
URL myObj = new URL("http://www.openloop.com/");
BufferedReader dis = new BufferedReader(new InputStreamReader(myObj.openStream()));
String inputLine;
while ((inputLine = dis.readLine()) != null)
{
System.out.println(inputLine);
}
dis.close();
}
catch (MalformedURLException me)
{
System.out.println("MalformedURLException: " + me);
}
catch (IOException ioe)
{
System.out.println("IOException: " + ioe);
}
Lab
Write a Java application called Week6 that will
- Create an URL object with http as protocol and
www.yahoo.com as resource name.
- Parse the newly created URL object and print out
- protocol,
- host,
- filename,
- port and
- reference
- Then, read directly from the URL object and print out all
the contents from the input stream to the display
- Run the program by "java Week6 > tempfile",
tempfile will hold all the output
Note: Remember to use the try catch block when working with
URL and InputStream
Note: Our application *actually* get to the network and download
information !!!
Copyright
1996-2001 OpenLoop Computing. All rights reserved.