Network communications
[Extra 3]


In this extra unit we'll look at network communications using Java. As part of this we'll look more generally at how the internet and web work.


Powerpoint and audio

Further info:

HTML is one of those text formats we talked about last unit -- simple to understand and use, but relatively bulky compared with binary files. If you're interested in learning the basics of HTML, you can find a simple tutorial here: HTML. For more detailed tutorials on stylesheets etc. see w3schools.

As angle-brackets mean "this is a tag", we need special characters to represent these (> is >, for example), along with other characters. A list of the common ones can be found on the Wired site.

HTML is a derivative of the Standard Generalized Markup Language specification. Both HTML and the SGML specification heavily influenced the development of XML, which is widely used for data formatting.

 

We'll only cover Applets in terms of Java's web technology on this course. However, if you are interested in the other technologies, you can find information here:

Servlets (programs to produce web pages)
JavaServer Pages (programs to embed data in webpages)
Java Webstart (a way of getting your programs to run over the web)

In general Servlets and JSP aren't as widely used as other server-side technologies such as PHP, Perl, and even Javascript, but do offer some advantages in terms of speed and power in enterprise settings.


Quiz:

The inventor of the internet was __________________.


  1. Me
  2. Douglas Engelbart's mother
  3. Tim Berners-Lee
  4. Robert Kahn and Vinton Cerf

Well, Robert Kahn and Vinton Cerf probably have the best shot at the title, as they came up with TCIP/IP, but systems of interlinked computers pre-date that, so it was a real collaborative effort. Tim Berners-Lee invented the web, rather than the internet. Douglas Engelbart was an early proponent of Hypertext, but also introduced a number of other computing innovations during The Mother of All Demos. You were Time Person of the Year in 2006 for filling the web with LOLCats and Dancing Hamsters. Congratulations.


Powerpoint and audio

<-- Example applet
If you can't see it, see security details below
(source: PyramidGraph.java; PyramidCanvas.java)

Further info:

A couple of useful tips when running applets from local files, rather than over the web.

Firstly, you may find that they don't run if your security is set to block unsigned applets by default (you may find this for all applets). You can lower the security if you need to (it just asks you if you want to run each applet; just remember to watch out for applets on untrusted pages). To lower the security, go into the Java control console (in Windows this is in Control Panel listed under "Java"), go to the security tab, and drop the level to medium then OK this and restart your browser.

Secondly, when you're developing in some browsers you may find that the browser "caches" the applet. This is when the browser keeps a local copy of something it is using so it doesn't need to go back to the internet to find it. This is usually useful, but not if you've changed the code in your applet class. If you find changes don't appear when you've reloaded the page, try restarting your web browser. Firefox, in particular, seems very reluctant to reload applets, and IE is actually better for this.

You can turn off the Java support for most browsers temporarily to see the effect on your web pages:
Details for Firefox can be found here.
In IE: Gear-shaped menu > Internet Options > Security Tab > Custom Level > Scripting > Scripting of Java Applets > Check Disable option.

You can find out more about the accessibility built into Swing on this Oracle site.

 

Applets were generally used for building complex user interfaces into webpages, however, this function is increasingly being taken on by Javascript, not least because of increasingly forceful security models that require more stringent updates than for Javascript engines. In general, it seems likely that the ultimate use of applets will be high-powered client processing, especially processing that utilises users local harddrives through signed applets.

We won't cover signing Applets in this course; for more information, see the Oracle page on this.

For more information on the Java Plugin, and how to set it up, see the Plugin webpage.

For more information on the jar tool, see the tool's webpage.

For info on HTML frames, see w3schools, though note that these are theoretically being replaced with iframes (browsers are unlikely to get rid of the anytime soon for back-compatibility).


Quiz:

An applet is most accurately described as _____________.

  1. a small application, usually running within another program
  2. a piece of java code that runs on a server and injects text into a webpage
  3. a way of starting standard java applications over the web
  4. a piece of java code that runs inside a webpage
  5. an apple, but smaller, juicier, and cuter

Correct! Actually, the term 'applet' really has a much wider applicability, though these days the term is almost always used in practice to mean java code that runs inside a webpage. Answers two and three are a "servlet" and "java web start" respectively. A small apple is called an iPad, obviously.


Powerpoint and audio

Further info:

List of commonly taken port numbers

Intro to TCP/IP

More on sockets

Manual DNS Stuff:
What'sMyIP?
DNS lookups

 

BTW, when testing internet technology, it is useful to know that the IP address 127.0.0.1 usually refers to the LOCALHOST, that is, the machine you're on. Communications sent out to 127.0.0.1 are just directed straight into the local machine's TCP/IP input.

 

With sockets you need to make sure the server socket code is running before trying to connect to it or the client will fail. One question we might then ask is whether there are ways of running server socket code on request. The answer is "kinda". The simplest option is to get a webserver to run the java code on request, i.e. use Servlets, however, there is also the java Remote Machine Invocation (RMI) architecture, or something like CORBA, for which there's a java implementation built into the JDK (ORB). Both require a slightly different kind of server to be running on the server machine, and, in general, most people now get more common web technologies to run the java for them as a standard application on the server if they don't want it running all the time.


Quiz:
Given the code below (ignoring missing try-catch blocks):

Client:
byte[] addr = {127,0,0,1};
InetAddress i = InetAddress.getByAddress(addr);
Socket s = new Socket(i, 9999);

Server:
ServerSocket ss = new ServerSocket(9999);
Socket s = ss.accept();

The exception thrown if we ran the client before we'd started the server would be a _____________.

  1. java.net.ConnectException
  2. java.net.SecurityException
  3. java.net.NullPointerException

Correct! It's java.net.ConnectException -- a sub-class of java.io.IOException, though that's almost impossible to work out from the docs; you really have to try it to see this (for some example code, see the "key ideas" page, below). You definitely need to have the server socket software up and running before you run the client. Alternatives to this are using servlets or RMI to start the server socket code (see above).


[Key ideas from this unit]