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.
-
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.
-
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!!!
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.
|
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.
|
getInputStream() | This is a method of the URLConnection class. Will retrieve an InputStream to data I/O with the corresponding Internet/URL file. |
Using URL to get to remote items: 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.
|
Issues in Creating a Chat Program:
Discussion of Client-Server Chat
Chat Related Links
|