Networking

 

 

 

Foreword

Resources

Code Listings

Foil Sets

Assignments

Solutions

External Resources

SiteMap

Search

Home

 

Up ] Thread Base Applet ] Math Class ] String Classes ] Off Screen GC ] JavaApplet Context ] Events ] [ Networking ] Java Appl Design ]

Networking in JAVA

Networking in Java is fairly seamless, you can not really tell if you are reading from a local file or a file in remote side. That is after you've opened the connection.

Note: There are restrictions, of course. Java applets cannot read or write from the disk on the mathine that's running them. Depending on the browser, Java applets may not be able to connect to systems other than the one upon which they were originally stored. Even given these restrictions, you can still accomplish a great deal and take advantage of the Web to read and process information over the net.

URL

Uniform Resource Locators are used on the web as a string identifier for a resource, rather that IP address. In "Introduction to Applet Context", we have a very simple example that show you about the showDocument() method. The showDocument() method require an URL argument. The code below will show you how to initialize an URL class.

    try {
      URL url = new URL(getCodeBase(), "Context.java");
      getAppletContext().showDocument(url);
    } catch (MalformedURLException e);
  

The getCodeBase() will return the current Java Code's URL. the "Context.java" is the file name or a path relative to URL. If you prefer an absolute URL, you can try:

    URL url = new URL("http://www.npac.syr.edu/index.html");
  

openStream()

URL defines a method called openStream(), which opens a network connection using the given URL and return an instance of the class InputStream (part of the java.io package). If you convert that stream to a DataInputStream (with a BufferedInputStream in the middle for better performance), you can then read characters and lines from the stream. For example, these lines open a connection to the URL stored in the variable url, and then read and echo each line of the file to the standard output:

    try {
      InputStream in = url.openStream();
      DataInputStream data =
        new DataInputStream(new BufferedInputStream(in));
      String line;
      while ((line = data.readline()) != null) {
        System.out.println("line")
      }
    } catch (IOException e) {
      System.out.println("IO Error:" + e.getMessage());
    }
  

The following is a sample that modify from "teach yourself JAVA in 21 days" that use openStream() method to read a file.

Source code : ReadStream.java

URLConnections

URL`s openStream() method is actually a simplified use of the URLConnection class. URLConnection class. URLConnection provides a way to retrieve files by using URLs - on Web or FTP sites, for example. URLConnection also enables you to create output streams if the protocol allow it.

To use a URL connection, you first create a new instance of the class URLConnection, set its parameters (whether it enables writing, for example), and then use the connect() method to open the connection. Keep in mind that, with a URL connection, the class handles the protocol for you based on the first part of the URL, so you don't have to make specific requests to retrieve a file; all you have to do is read it.

The following code can help you get an idea about how to open an connection and get the input stream from it. You can see it is almost identical to the openStream(), only more complex.

    try {
      URLConnection conn = url.openConnection();
      conn.connect();
      InputStream in = conn.getInputStream();
      DataInputStream data =
        new DataInputStream(new BufferedInputStream(in));
      String line;
      while ((line = data.readline()) != null) {
        System.out.println("line")
      }
    } catch (IOException e) {
      System.out.println("IO Error:" + e.getMessage());
    }
  

The following is a sample that modify from "teach yourself JAVA in 21 days" that use the URLConnection class to read a file.

Source code : ReadText.java