Key Ideas
[Part 10 of 11]


There are two key ideas in this part:

  1. The first is how to make applets. Here's HelloWorld as an applet, and the associated webpage. To run them, save them all in the same directory, compile the java file, and open the webpage by dragging and dropping it into a browser or opening it from the file menu. The code is listed below, but here it is in files for convenience: HelloWorld.java; index.html (you can see what it should look like by clicking on the index.html link).

     

    import java.applet.*;
    import java.awt.*;

    public class HelloWorld extends Applet {

       public void init () {
          setSize(200,200);
          setBackground(Color.LIGHT_GRAY);
          repaint();
       }

       public void paint (Graphics g) {
          g.drawString("Hello World", 50, 50);
       }

    }


    <HTML>
    <HEAD>
    <TITLE>Helllllooooo World</TITLE>
    </HEAD>

    <BODY>

    <APPLET
       code = HelloWorld
       width = 200
       height = 200
       align = left
    >

    <PARAM name="name1" value="attributeValue1">
    <PARAM name="name2" value="attributeValue2">

    </APPLET>

    </BODY>
    </HTML>

     


  2. The second is that other kinds of network communication can be achieved using sockets. Here, again, is some example code and the source code: Server.java; Client.java. In the below, we've removed the try-catches and only listed the constructors/instance variables for clarity. Download the files and compile them. To run them on the same machine, open two command prompts, run the Server.java on one first, then start the Client.java on the other. Type at the client command prompt and push enter, and you should see the text appear on the server side. To stop both client and server, enter the character noted on the client when it runs and press enter (usually the "logical negation" symbol "¬" found on the key before "1" on British keyboards; if this doesn't work, or the character listed isn't on your keyboard, see the comments in the class file).

    To run it across machines, get the IP address of the machine that's going to be the server and add it to the Client.java code where it is currently set to 127,0,0,1 and recompile. Run Server.java on the server machine, and Client.java on the client machine. You should be able to send messages from client to server, firewall allowing. If you want to return messages, try running the other class on each machine in two new command prompts, but with a different port number.

     

    private byte[] addr = {127,0,0,1}; // 127.0.0.1 is LOCALHOST.

    public Client() {

       InetAddress i = InetAddress.getByAddress(addr);
       Socket s = new Socket(i, 9999);
       BufferedOutputStream b = new BufferedOutputStream(s.getOutputStream());

       int c = 0;
       while ((c = System.in.read()) != 170) {
          b.write(c);
          b.flush();
       }
       s.close();

    }


    public Server() {

       ServerSocket ss = new ServerSocket(9999);
       Socket s = ss.accept();
       BufferedInputStream in = new BufferedInputStream(s.getInputStream());

       int c = 0;
       while ((c = in.read()) > -1) {
          System.out.print((char)c);
       }
       in.close();

    }

     

    Note that because sockets work on a binary stream, here we used the same code for reading binary streams that we saw in the file-reading lecture. When we output, we cast the ints that come in into char. Note that this should only work in a stable fashion with characters where the character values can fit into a byte, i.e. the 8 bit ISO Latin-1 Character Set extension to ASCII, because the int c is only filled completely when it gets the value -1 at the end of the stream; up until this point, only a byte at a time is read into it.

  3.