Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


CHAPTER 40
Network Programming

by Mike Morgan

In this chapter
Java Simplifies Network Programming 1122
Connecting to the Internet: The URL Class 1122
The Java Socket Classes 1126
How the Internet Uses Sockets 1126
Writing Your Own Client and Server 1128
Communicating with Datagram Sockets 1133
Customized Network Solutions 1136
Will Security Considerations Disable Java Applets? 1137
Using Network Communication in Applets 1138

Java Simplifies Network Programming

Not so long ago, programming network applications in any language was an ordeal. Sometimes it involved writing specialized system software that talked directly to network drivers—or even the network cards themselves. Programming IPX/SPX applications in DOS or Windows used to require the programmer to write software interrupt handlers. With Java, however, writing some kinds of network applications is as easy as using println().

The thing that makes network programming in Java easier is encapsulation. Java hides the difficult, low-level network programming from you. This design enables you to concentrate on your application, not on the communications. In older systems, many steps were required to talk over a network. You had to

  Initialize the network card
  Set up buffers for inbound and outbound packets
  Create callback routines for the networking driver to notify you of data
  Write low-level (sometimes assembler!) code to talk with the network driver

If it sounds like a lot of work—it was! On the other hand, programming the network in Java is very slick. Just instantiate a Socket class and you are on your way. But more on that later.

This chapter discusses the network classes in Java (in the java.net package). These classes make writing programs for communication over the Internet, intranets, or even local area networks easier than in any other language you’re likely to use.


How Do Java Programs Communicate with the Outside World?

Java’s network classes use streams for their underlying communications. A stream is a path of communication between a source of information and a destination. For our purposes, our Java program is at one end of the stream. If we’re the source, the stream is an output stream. If we’re the destination, we call the stream an input stream.

Readers and writers are analogous to streams, except that streams are based on bytes, and readers and writers are based on chars. In older languages, such as C and C++, this distinction is not important. In Java, however, a char is a 16-bit entity designed to hold Unicode characters.

You’ve already used an output stream without realizing it, when you wrote System.out.println(“Hello, world!”). Streams enable you to communicate with files, printers, the screen, and the network. Streams, readers, and writers are all part of the java.io package. You can find complete documentation on each class, interface, and method in /docs/api/Package-java-io.html in your JDK directory. You’ll also need to import java.io.* or specify java.io as part of the class name in your code.


Connecting to the Internet: The URL Class

Back in Chapter 37, “Developing Java Applets,” you learned how to write a tiny Java program—HelloWorld.java—with only one executable statement. The fact is, people have been writing tiny programs that write information to the screen ever since there were computer screens. One of the features that makes Java exciting is that you can connect to the Internet with a program that’s not much more complex than HelloWorld.java.


NOTE:  For years Sun has been telling us that “the network is the computer,” and its design of Java reflects that philosophy. Java is the only major language that enables you to connect your program to the Internet in just one line of code, as shown in line 39 of Listing 40.1:

URL theURL = new URL(theURLString);


  SeeHelloWorld.java in Listing 37.1, p. 990.

Using showDocument() to Change Web Pages

Listing 40.1 shows HelloNet.java, a simple applet that connects you to a new Web page.

Listing 40.1 HelloNet.javaThis Simple Applet Connects to Three URLs


 import java.awt.*;
 import java.net.*;
 import java.applet.Applet;
 import java.awt.event.*;
   public class HelloNet extends Applet
 {
   public void init()
   {
     setLayout(new GridLayout(3,1));
     Button theBookSiteButton = new Button(“Platinum Edition”);
    theBookSiteButton.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e) {
         linkTo(“<http://www.mcp.com/> info/0-7897/0-7897-1759-X”);
       }
     });
     add(theBookSiteButton);

     Button theMCPButton = new Button(“Macmillan Computer Publishing”);
     theMCPButton.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e) {
         linkTo(“<http://www.mcp.com/>”);
       }
     });
     add(theMCPButton);

     Button theJavaSoftButton = new Button(“JavaSoft”);
     theJavaSoftButton.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e) {
         linkTo(“<http://java.sun.com/>”);
       }
     });
     add(theJavaSoftButton);
   }
   public void linkTo(String theURLString)
   {
     try {
       URL theURL = new URL(theURLString);
       getAppletContext().showDocument(theURL, “_top”);
     } catch (MalformedURLException e) {
       System.err.println(“Bad URL: “ + theURLString);
     }
   }
 }


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.