Here is a link to the presentation on ad blocking
Here are links to many of the classes that we will need.
ServerSocket
The second constructor, ServerSocket(int port) creates a server socket, binds it to the local host at the the requested
The other important method that we need is accept. This blocks until a client connects, and then returns a Socket
When you look at the documentation for the constructor, you see this. public ServerSocket() throws IOException
Exceptions are error conditions or other problematic situations
The syntax is
try { // stuff that could cause exceptions } catch (exception1) { // what to do if exception1 occurs } catch (exception2) { // what to do if exception2 occurs } finally { // things to do even if exceptions occur }There are two kinds of exceptions, checked and unchecked. The compiler forces you to handle the former. You must tell the compiler what you will do if it occurs. The latter should be checked, but is not enforced. Checked errors are those that could occur no matter how careful programmer is. (Trying to open a file which is not there) while the latter are generally the programmer's fault (ArrayBounds exception)
An uncaught exception will crash your program
Here is a link to the on-line help for Exception
The accept member of ServerSocket returns a Socket. Here is the complete description.
Pay particular attention to the Socket member function getInputStream(). This returns an instance of the class InputStream.. This is an abstract superclass which means that it cannot be instantiated, it can only be used as a base class to derive other classes.
There are two ways to store data, in text or binary, so java has two types, char and byte. Input and output streams are bytes.
To read or write characters, you need the classes reader and writer.
There is a class InputStreamReader which converts an input stream (bytes) to a reader (chars)
A charset is named mapping between sequences of sixteen-bit Unicode code units and sequences of bytes. This class defines methods for creating decoders and encoders and for retrieving the various names associated with a charset. Here are a few.
US-ASCII Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set ISO-8859-1 ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1 UTF-8 Eight-bit UCS Transformation Format UTF-16BE Sixteen-bit UCS Transformation Format, big-endian byte order UTF-16LE Sixteen-bit UCS Transformation Format, little-endian byte order UTF-16 Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order markIf you don't specify the charset when you create a reader, it uses the default, which on our systems is US-ASCII. Here is some code.
try { Socket s = new Socket(``www.cs.rpi.edu'', 80); InputStream in = s.getInputStream(); InputStreamReader isr = new InputStreamReader(in); BufferedReader br = new BufferedReader(isr); int c; while ((c = br.read()) != -1) { System.out.print((char) c); } System.out.println(); } catch (IOException ex) { System.err.println("Error reading from socket"); }Output Stream is also an abstract superclass, so we need to choose a subclass
Here is the code for server.java
To compile and run the server on your laptop, open a command prompt
window, cd to the directory where the source code is, and type
javac server.java
It is likely that you will get a message
javac is not recognized as an internal or external command
This means that you need to type the absolute pathname
of the compiler. On my system it is
c:\jdk1.3.1_01\bin\javac server.java
.
(Use the Windows search function to look for javac.exe)
javac
is the java compiler. If you get compiler errors,
fix them and try again (my code should not have compiler errors).
Once you have a clean compile, there will be a file called
server.class in your directory. To run this, type this
command
java server 44444
(The last arg is the port.)
Like a good server, nothing happens until a client connects.
In general, a URL can be broken into several parts. Here is a typical url.
http://java.sun.com:80/index.html#chapter1
This has five fields
There is also a member InputStream openStream() which actually opens a connection to this url. this.
Here is a sample program which demonstrates this. Note that it downloads the course web page without the programmer having to create a socket.
Here is code for a datagram socket server