Network
Network
Dr Andy Evans
Applets.
URLs.
Advanced Applets.
Webpages.
Network communications.
The Art of Programming Part Ten: Working with webpages.
Next week.
The web
You can, however, “sign” applets. If the user trusts you the
applets can be given more freedom.
Basics
All applets are GUI based and all but simplest are Event Based.
Therefore they also need to import java.awt.* and
java.awt.event.*.
Methods
stop()
Called when a browser leaves the webpage.
destroy()
Called when the browser kills the applet - ‘stop’ is always called
first. Not usually possible to say when this happens.
import java.applet.*;
import java.awt.*
import java.awt.event.*
}
System.out is the browser’s Java Console.
Applets.
URLs.
Advanced Applets.
Webpages.
Network communications.
The Art of Programming Part Ten: Working with webpages.
Next week.
java.net.URL
Encapsulates a Uniform Resource Locator (URL).
http://www.w3.org:80/People/Berners-Lee/Overview.html
Which server program gets which requests will depend on the port
they are sent to, which is usually related to the transmission
protocol.
e-mails are sent out to servers using Simple Mail Transfer Protocol
(SMTP) which usually goes into Port 25.
You can then use one of the URL class constructors. Some let
you put in the port, or split off the path.
Easiest is…
getFile()
Gets the file name of this URL.
getHost()
Gets the host name of this URL, if applicable.
getPath()
Gets the path part of this URL.
getPort()
Gets the port number (int) of this URL.
Applets.
URLs.
Advanced Applets.
Webpages.
Network communications.
The Art of Programming Part Ten: Working with webpages.
Next week.
Advanced Applet methods
getImage(URL)
Returns an java.awt.Image object from a given
URL object.
getDocumentBase()
Returns a URL object representing the directory the
applet starting webpage was in.
getAppletContext()
Returns an AppletContext object representing the environment the
applet is running in.
AppletContext ap = getAppletContext();
showDocument(URL webpage).
This allows you to put a webpage into the browser window.
ap.showDocument(new URL(“http://www.w3.org/”))
Applets.
URLs.
Advanced Applets.
Webpages.
Network communications.
The Art of Programming Part Ten: Working with webpages.
Next week.
Webpages
They consist of text that will be displayed and tags that won’t, which
include formatting details or references to other files like images or
applets.
Saved as text files with the suffix .html (or sometimes .htm)
A basic webpage
<HTML>
<HEAD>
<TITLE>Title for top of browser</TITLE>
</HEAD>
<BODY>
<!—Stuff goes here--!>
</BODY>
</HTML>
The HEAD contains information about the page, the BODY contains
the actual information to make the page.
<BODY>
The break tag breaks a line<BR>
like that.
<P>
The paragraph tags
</P>
leave a line.
This is <B>Bold</B>.
This is <I>Italic</I>
</APPLET>
PARAMs
We can pull in the parameters using the applet’s
getParameter(String paramName) method, which returns the
values associated with paramName as a String. For example, in our
web page we might have:
if (colorScheme.equals("1")) {
setBackground(Color.WHITE);
}
<OBJECT
codetype="application/java"
classid= appletFile
width = widthInPixels
height = heightInPixels
>
appFrame.add(twoFaced, BorderLayout.CENTER);
appFrame.setSize(300, 300);
appFrame.setVisible(true);
}
Applets.
URLs.
Advanced Applets.
Webpages.
Network communications.
The Art of Programming Part Ten: Working with webpages.
Next week.
Introduction to network communications
Communication is via a port, using a protocol. Several protocols may
be involved at once. Most use the following over the internet…
Internet Protocol (IP)
Used to split data into small chunks called “packets”
Addresses them to the right machine.
java.net.InetAddress
InetAddress ina =
InetAddress.getByName(hostNameString);
Returns an InetAddress corresponding to the host
named.
InetAddress[] ina =
InetAddress.getAllByName(siteNameString);
Returns an array of InetAddress objects containing all
the servers associated with a given site name.
InetAddress methods
Once you have your address, you can then connect to it.
To do this, we use “sockets”.
These are TCP/IP based stream connections to a computer
port. You need a client and a server.
java.net.Socket
Sends requests to communicate to a port using TCP or equivalent.
java.net.ServerSocket
Lets you build your own server.
Sits and listens to a port in case something calls it.
Sockets
TCP/IP reserves the first 1024 ports, otherwise you can use most
numbers up to 49151.
Once you have your sockets connected, you can use their
getInputStream(), getOutputStream() and close() methods to
talk using streams.
Sockets example
On the server…
On the client…
Socket s = new Socket(InetServer, 9999);
BufferedOutputStream b = s.getOutputStream();
b.write(aByteArray);
s.close();
java.net.URLConnection
If you want specific files from an http server which exists, it’s
easier to use a URLConnection object.
Methods include…
getDate()
getContentLength()
getInputStream()
Knowing the number of bytes of a resource (the
ContentLength), and having an InputStream, you can now read
the file.
Resource Streams
</APPLET>
When writing for the network, you can compress class files and
associated resources using the jdk1.7/bin/jar.exe compressor. This
creates “zip”-like files that can be read in java using java.util.zip
or Class’s getResource().
<APPLET
code = myClass
archive = myJar.jar
width = 300 height = 300>
</APPLET>
Transferring files
Always check what other programs, if any, use the port you
are on.