CONNECT TO THE INTERNET
No matter what hardware you're using to connect to the Internet -- dial-up modem, ISDN (Integrated Services Digital Network), cable, and optical fiber -- you only have to deal with TCP/IP and URLs at the programming level.
TCP/IP (Transmission Control Protocol/ Internet Protocol) can be explained as this: IP is responsible for getting chunks of your data to the computer you are addressing, and TCP is responsible for making the overall communication process reliable.
A URL (Uniform Resource Locator) is a standardized way of addressing resources on the Internet, and is also know as a URI (Uniform Resource Identifier). The standard is maintained by the W3C. A URL (or URI, if you prefer) is composed of at least two parts -- a protocol plus some form of address, optionally followed by additional data. Table 7-1 shows some common Internet protocols and how they're used.
| Symbol |
Protocol
|
Used for |
| http: |
Hypertext Transfer Protocol
|
Web resources including hyperlinks |
| ftp: |
File Transfer Protocol
|
Standard utility for file transfers |
| telnet: |
Terminal Emulator
|
Character by character terminal emulation for remote access |
Table 7-1: Common Internet protocols.
A Simple Internet Example
Java is an ideal language for experimenting with grabbing information off the Internet because it was designed from the beginning to be network aware. The following example demonstrates connecting to a well-known site run by the United States government to provide accurate time. It uses the following URL:
http://tycho.usno.navy.mil/cgi-bin/timer.pl
The parts of this URL are:
- http: This declares the protocol used.
- tycho.usno.navy.mil: This is the Internet address of a particular computer. You know this is a military network site because of the .mil.
- cgi-bin/timer.pl: This says that you want a response from the timer.pl function, which happens to be a script in the PERL scripting language.
We're going to write a Java program to connect to that computer, access that timer.pl script, and capture the output. To keep the example compact, the following program accomplishes everything in a single method -- not a particularly good practice for anything larger than this example. This program is similar to sequential file reading, as discussed in Lesson 6, except that you use a URL instead of a local file name. The line numbers are for reference only.
1. package com.pb ; 2. import java.net.* ; // note 3. import java.io.* ;
4. public class UrlExample{
5. public static void main( String[] args ){
6. if( args.length < 1 ){ //
7. System.out.println(
8. "Program expects a URL on the
command line");
9. System.exit(1);
10. }
11. try { //
12. URL theUrl = new URL( args[0] ); //
13. URLConnection conn =
theUrl.openConnection();
14. conn.connect();
15. InputStream in = conn.getInputStream(); //
16. int ch = in.read();
17. while( ch != -1 ) { //
18. System.out.print((char)ch );
19. ch = in.read();
20. }
21. in.close();
22. System.out.println("Done");
23. }catch(Exception e){ //
24. System.out.println("Problem " +
e.toString() );
25. }
26. }
27. }
- Line 6: This is the way a Java program gets information from the command that started it. The program expects to see an array of strings that have at least one item, namely the URL, to connect to. If there's nothing there, you get the error message on Line 8.
- Line 11: This block of code starts with try and ends with catch . It's part of Java's error notification system.
- Line 12: This statement creates an object that knows how to create a connection to another computer using a URL. The connection is represented by a URLConnection object that gets created on Line 13.
- Line 15: The InputStream object represents the way Java reads streams of characters. This object behaves the same way when reading from the connection as it would when reading from a local file.
- Line 17: Read characters from the stream one at a time and write them out until you reach the end, where the end is indicated by getting a -1 from the InputStream .
- Line 23: The code would get executed if the statements between the try and here cause an error, such as not being able to contact the URL.
When executing a program such as this, with the URL on the command line:
java com.pb.UrlExample http://tycho.usno.navy.mil/cgi-bin/timer.pl
you get the following response:
<title>What time is it?</title> <h2> US Naval Observatory Master Clock Time</h2> <h3> <br><br>June 16, 01:43:28 UTC <br><br>June 15, 09:43:28 PM EDT <br><br>June 15, 08:43:28 PM CDT <br><br>June 15, 07:43:28 PM MDT <br><br>June 15, 06:43:28 PM PDT Done
Isn't that cool? That's all it takes to grab information off a computer on the Internet.
Web Services
Naturally, things can get a lot more complicated than that simple example. A hot topic these days is the idea of a Web service . A Web service is a program that's accessible from a network such as the Internet, and has a well-defined set of functions it can perform on request. XML, the markup language discussed in Lesson 5, is commonly used in creating Web services.
