Caught in the Net

A sip of Networking

" Oh good! You're awake"

If you thinks a web is that weelittle fly-catching thing in the corner of your room, you've probably been living in the Himalayas. Today, each and every little Tyke (and his dog), are on the World Wide web. And if you are reading this, then more certainly than your dog's name (Bruno?), you are conversant with the powers of this blown-up scaffold of computer networks. The bad news is that if you feel writing programs in the field of networking, say for something like a live world-wide chat, is a nightmare, then you are probably feeling right. The good news, however, is that before a couple of shakes of Bruno's tail, you will be writing your first Networking program! The program that we'll build on right now, is to yank in a document lurking somewhere on the web (doesn't everything just lurk somewhere out there?) into our computer. And if its kinda hard-hitting, try not to weasel out midway; it will all begin to make sense soon.

Albeit an elaborate explanation is absconding now, we expect you to try this out. Meanwhile, we will do some more slogging :)

Before we swoop down on the actual clutter of Resource Locators, servers, streams etc., let us remind ourselves of a basic list and a button.


Program 1

import java.awt.*;
import java.applet.*;
public class zzz extends Applet
{	public void init()
	{	setLayout(new BorderLayout());
		List l;
		l = new List(3000, false);
		add("Center", l);
		add("North", new Button("Start"));
	}
}

Now, let us write the novice program.
Program 2
import java.awt.*;
import java.applet.*;
public class zzz extends Applet
{	URL u;
	public void init()
	{	setLayout(new BorderLayout());
		List l;
		l = new List(3000, false);
		add("Center", l);
		add("North",new Button("Start"));
	}
	public boolean action ( Event e, Object o)
	{	if ( "Start".equals(o) )
		{	try
			{	u=new URL("http://www.neca.com/~vmis/java.html");
				showStatus(" URL is initialised ");
			}
			catch(MalformedURLException x)
			{	showStatus("error in URL");
			}
		}
		return true;
	}
}
Not as bed-wettingly scary as you thought it would be, was it? Only the vocabulary of the programming luminaries seems to be bloated with jargon. If you've been using the web, the kinda highfalutin word URL (Uniform Resource Locator) is perhaps at least halfway memorable by now (we wonder why it is called a URL, but we understand one can live out a full and relatively cheerful life without ever needing to know) Anyway, you will get these errors as of now...

class URL not found in type declaration
class URL not found in new
class MalformedURLException not found in type declaration
class MalformedURLException not found in catch

Therefore, we use the inevitable import , which, incidentally, is mandatory if you want to get past the compilation stage!

import java.net.*;
With that wisdom up our sleeve, let us give it another try. We first have to define a URL connection . Right after the line that says URL u , let us add this line
URLConnection c;
And later, in the action() , we write the following...
			
try
{	c = u.openConnection();
	showStatus("Open Connection successful");
}
catch(IOException x)
{	showStatus ("Connection failed");
}
But again, there are three errors lobbed at us which very rhetorically go as...

Exception :java.io.IOException must be caught , or it must be declared in the throws clause of this method
Class IOException not found in type declaration
Class IOException not found in catch

And that brings us to yet another import statement. This one is for the IO (and for those who came in late or didn't come at all, that stands for "Input Output").

 import java.io.*;
As yet, whatever we are doing with our code is far from spectacular. While we are taking care of the URL and the URL connection, we conveniently forget about fetching in the file. Towards that end, we have a class called InputStream . Hence, we include the following line...
	InputStream n;
Besides, our action() will now look like this...
	public boolean action ( Event e, Object o)
	{	if ( "Start".equals(o) )
		{	try
			{	u=new URL("http://www.neca.com/~vmis/java.html");
				showStatus(" URL is initialised ");
			}
			catch(Exception x)
			{	showStatus("URL not initialised");
				return true;
			}
			try
			{	c = u.openConnection();
				showStatus("Open Connection successful");
			}
			catch(Exception x)
			{	showStatus("Error in Open Connection");
				return true;
			}
			try
			{	n = c.getInputStream();
				showStatus("getInputStream successful");
			}
			catch(Exception x)
			{	showStatus("error in getInputStream");
				return true;
			}
		}
		return false;
	}


Next we have the DataInputStream class which will likewise be inserted in the following way...

	DataInputStream d;
The action() will now become...
	public boolean action ( Event e, Object o)
	{	if ( "Start".equals(o) )
		{	try
			{	u=new URL("http://www.neca.com/~vmis/java.html");
				showStatus(" URL is initialised ");
				c = u.openConnection();
				showStatus("Open Connection successful");
				n = c.getInputStream();
				showStatus("Input Stream successful");
				d=new DataInputStream(n);
				showStatus(" Data Input Stream successful");
			}
			catch(Exception x)
			{	showStatus("error in URL/Connection");
			}
			return true;
		}
		return false;
	}

Note that this time we have a single catch for all the exceptions instead of having a barrage of them (though having them seperately shown is good if you want to see the exceptions explicitly). Let us also have a String and place it in the action() .
String s;
action()
try                              
{	s=d.readLine();
	showStatus ("String "+s);
}
catch(Exception x)
{	showStatus("String not read");
	return true;
}
Let us add this to the list.
try
{	l.addItem(s);
	d.close ();
	n.close(); 
}
catch(Exception x)
{	showStatus("Add Item not successful");
	return true;
}
It must be at least halfway clear that the string is being used to display inside the listwhatever has come in as a file. But the problem here is that the file is not being properly read. We'll have a loop for the reading, so that the complete document has been read.


public class zzz extends Applet
{	URL u;
	URLConnection c;
	InputStream n;
	DataInputStream d;
	String s;
	List l;
	public void init()
	{	setLayout(new BorderLayout());
		l = new List(3000,false);
		add("Center",l);
		add("North",new Button("Start"));
	}
	public boolean action ( Event e, Object o)
	{	if ( "Start".equals(o) )
		{	try
			{	u=new URL("http://www.neca.com/~vmis/java.html");
				showStatus(" URL is initialised ");
				c = u.openConnection();
				showStatus("Open Connection successful");
			}
			catch(Exception x)
			{	showStatus("error in URL/Connection");
			}
			try
			{	n = c.getInputStream();
				showStatus("Input Stream Open successful");
			}
			catch(Exception x)
			{	showStatus("error in get Input Stream");
				return true;
			}
			try
			{	d=new DataInputStream(n);
				showStatus(" Data Input Stream Open successful");
			}
			catch(Exception x)
			{	showStatus ("error in Data Input Stream");
				return true;
			}
			try
			{	while ((s=d.readLine()) != null )
					l.addItem(s);
				n.close();
			}
			catch(Exception x)
			{	showStatus("Add Item not successful");
				return true;
			}
		}
		return false;
	}
}


Unless its time to take Bruno for a walk, get to the next topic "Miscellaneous Code Dump" or just reach your canine gnarls :) i.e., your comments, feedback, suggestions et al to us before he begins to claw at your door.

Back to our Java page


Vijay Mukhi's Computer Institute
B-13, Everest Building, Tardeo, Bombay 400 034, India.
http://www.neca.com/~vmis
e-mail : vmukhi@giasbm01.vsnl.net.in
1