Communication Across Internet
-
You can not only perform file I/O with the machine containing the java
application but, also with files/systems accross the Internet.
-
Sometimes referred to as making your application "Net-aware".
-
Examples:
-
load a document from over the Internet,
-
create client-server program,
-
create distributed computer programs.
Java Applet Limitations:
1) cannot read or write from the disk on the machine where the browser
is running.
2) cannot connect to systems other than the one on which they were
originally stored.
3) If you try to load files off of the system where the applet is originally
stored, you will get a security exception and other error messages.
Java Applet Possibilities:
1) You can read in the contents of files on the system where
the applet is originally stored.
-
Do so by refering to the file by its URL (Universal Resource Locator),
-
Note: if you do this kind of I/O communications in an applet, you
can no longer use the appletviewer tool to test it -> you must
test it inside of a Web browser AND have the applet on a web server
along with the HTML that contains it!!!
2) Something new to look out for that will extend the applet conectivity
possibilities: "secure applets".
Package = java.net
java.net Classes and important methods
URL |
This is a class that represents a URL. Can create an URLConnection
object with its openConnection() method. |
URLConnection |
This class represents a connection with a URL. Can use this to
get an Inputer stream (getInputStream() method) to read/write information. |
Socket |
Abstraction of standard TCP socket. Provides a client-side socket
interface similar to a standard UNIX socket. This provides Internet
connectivity beyond URL and URLConnection classes.
-
Socket(hostName, portNum)
-
must know hostname and portnumber you want to make a connection with.
Portnumbers often indicate the kind of protocol communications you will
be performing, e.g. ftp versus telnet.
-
Open a connnection on this socket using getInputStream() as done in URL/URLConnection
example below.
-
Once a socket is open you can use I/O streams like in URL/URLConnection
example below.
-
close()
-
make sure you close your socket when you are done with your communications
(I/O).
|
ServerSocket |
In this case, a server-socket does not open a connection but instead
has a method action() that waits and listens on the TCP port for a connection
request from a client.
-
In this way, you can create dual-applications: one a client Socket that
asks for a connection and the other a ServerSocket that will accept making
a connection with it.
|
getInputStream() |
This is a method of the URLConnection class. Will retrieve an
InputStream to data I/O with the corresponding Internet/URL file. |
|
|
The Steps:
1) Create a URL object representing file
2) Create an URLConnection object that can load
this URL and make a connection to the site hosting it.
3) Using getInputStream() method of the URLConnection
object, create an InputStreamReader that can read from this URL.
4) If you wish create a different kind of reader,
e.g. BufferedReader, as we did before with File I/O to more efficiently
read the data as you wish.
|
|