We start out finished threads on
time this year...
I. Networking: It's all just bits!! (zeros
& ones)
- What does it take to make the internet?
- computers,
- phone lines, (or some other means of transmission:
wireless (radio), optical)
- protocols
(see a
definition).
- (OK, modems help too...)
- hardware devices to translate the phone information into
computer bits & back.
- All information in a computer is just bits.
- 0s & 1s.
- This is true in memory, and also going across a phone line.
- When you come down to it, there's not much difference between a
disk & a phone line from the perspective of a CPU.
- You can think of networking as being pretty much like file I/O.
- If an object is serializable for files, it's serializable for
networking.
- Most basic objects implement serializable. For example,
Component does, so all the widgets in GUIs are serializable.
- The biggest difference between networking & files is that
even more can go wrong.
- Information gets lost or corrupted.
- Phone lines are noisy.
- So have to have redundant information, ways to check if
something changed, ways to ask for things that have gotten lost or
damaged.
- A protocol is a system for communicating between two computers.
- Happens at many levels of abstraction.
- Application protocols are often human readable so that they can
be
debugged. But lower level protocols are only for machines.
- Will learn about these in two weeks.
- Will talk more about how the internet works in general in two
weeks.
- Today we're going to just talk about the most basic case: two computers
talking to each other.
II. Clients & Servers.
- Sometime (in the 80's?) people noticed you didn't have to have
one big expensive computer do everything.
- Computers could be specialized to a task.
- Modularity: making programs smaller makes the easier to code,
debug & maintain.
- A basic pattern: client / server.
- Server provides a service needed by many people, programs or
computers. E.g.
- file server
- database server
- web server
- Clients use the services.
- Now-a-days at least clients usually run locally on a computer
used exclusively by the user.
- But in the old days, some people used this architecture even
if everyone was on the same computer:
- made the code cleaner, and/or
- provided single point for security, file integrity etc.
- For coursework 2, you will make a chat program with a client
& server.
- The main difference between the client &
the server will be which is already running (server) & which goes
looking for the server to attach to (client.)
- Once they are connected, they both behave the same way.
- Server listens for a client to contact it, then connects the
two programs.
- Servers often provide multiple connections: if you are
going to try to have multiple chat lines, you'll probably want the
server to do the coordinating.
- Networking works almost the same if both programs are on the same
machine as if they are on a different machine.
- The main difference from a programming perspective is that the
address you use for finding the other program is longer.
- Given what I said about incremental design, you should probably
try to make networking work on the same machine before you try to get
it working across different machines.
III. Sockets & Java
- Sockets are the most basic networking thing you need in Java.
- They exist in any networking-capable language.
- Java hides more of the complexity / makes networking easier to
use than older languages.
- The sort of virtual line that connects two programs is called a pipe.
- In unix/linux you can create a pipe between two programs on the
command line using "|" -- so that character is often called a pipe.
- unix programs have three sort of `automatic' sockets, named
STDIN, STDOUT, STDERR
- (standard in, standard out, and standard error)
- Normally STDIN gets the keyboard, STDOUT goes to the
terminal, but you can redirect these either into files or pipes.
- Example: %
egrep jjb sent-mail | wc
- Assuming % is the prompt.
- This finds all the lines in your file sent-mail that have the
word "jjb" in them, then pipes the output into a program that will
count the lines (& words & letters) so you can see how many
times you've emailed me.
- could also pipe it through "less" & see the lines on the
screen in a program that lets you go forwards & backwards through
the output.
- A socket
is what's at either end of a pipe.
- You can think of it as the hole in an application that data
goes through.
- A program will have one socket for every connection it has open
at one time.
- Often you might have two pipes going to the same other program
--- one for input & one for output.
- Of course, if pipe1 is input for application1, it is output
for application2!
- Then presumably pipe2 is output for application1 and input
for application2.
- Java sockets will handle both input & output for you, but
split these into two different streams.
- This is probably because people used to always get really
confused connecting their sockets up right...
- In Java, sockets live in java.net,
along with a whole lot of other useful networking things.
- Java's basic sockets provide a connection-oriented protocol which
will make sure no data is lost for you.
- connection is maintained even when no data is going down it.
- In particular, Java uses TCP (the
Transmission Control Protocol).
- This works on top of IP (the
Internet Protocol) which is what handles the packets
- packets are little chunks the data gets broken into so no big
/ slow chunk risks getting lost
- packets include some other useful information such as the
address they're going to.
- having the address in every packet is redundant, so the size
of packets has to be traded off with other networking considerations.
- To connect two applications, we need a full address,
- An IP
address, or a name to look it up with, and
- A port
number.
- A port number is an abstraction made up inside the
computer so that multiple applications can be talking over the network
at one time.
- Each socket has its own port number, so you can tell which
socket should get any particular infromation packet.
- Need to pick a port number that's not being used by anything
else! So choose a big one (e.g. more than 10000).
- Better if you can look -- on unix/linux look at the file
/etc/services.
- If anyone knows where to look on Microsoft, let me know...
- What you really want to do also is to write down that
you're using a port, but you guys won't have permission to do this...
but you should if you are building a real service for a company!
IV. Code
- These examples are taken from Learning Java.
- You might also want to see their complete sample programs for clients
& servers
(though more are coming Thursday!)
- Notice that most of the code below is showing various ways to
read & write to the socket, only one of which you'll need for
coursework 2.
- Notice also though that you have to be careful to always read
& write in the right order!
- It may still be easiest to use more than one socket...
// these examples are taken from Learning Java, pp. 335--337
// to open a client socket, we need to name the machine & the port the
// socket will be on. We also have to catch a couple exceptions...
try {
Socket sock = new Socket ("wupost.wustl.edu", 25);
} catch (UnknownHostException e) {
System.out.println("Can't find host.");
} catch ( IOException e) {
System.out.println("Error connecting to host.");
}
// Here's a client that reads & writes!
try {
Socket server = new Socket("foo.bar.com", 1234);
InputStream in = server.getInputStream();
OutputStream out = server.getOutputStream();
// write a byte
out.write(42);
// write a newline or carriage return delimited string
PrintWriter pout = new PrintWriter (out, true);
pout.println("Hello!");
//read a byte
byte back = (byte) in.read();
// read a newline or carriage return delimited string
BufferedReader bin =
new BufferedReader (new Input StreamReader(in));
String response = bin.readLine();
// send a serialized java object
ObjectOutputStream oout = new ObjectOutputStream (out);
oout.writeObject(new java.util.Date());
oout.flush();
server.close();
} catch (IOException e) {...} // every one of these operations could have died!
// server code (for the same conversation! Notice these have to be in
// the same order, only reversed...)
// assume this is running on the machine foo.bar.com
try {
// note: ServerSocket is a subclass of Socket that knows how to listen...
ServerSocket listener = new ServerSocket (1234);
while (!finished) {
Socket client = listener.accept (); // this waits for a connection
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
// read a byte
byte = someByte = (byte)in.read();
// read a newline or carriage-return-delimited string
BufferedReader bin =
new BufferedReader(new InputStreamReader(in));
String someString = bin.readLine();
// write a byte
out.write(43);
// say goodbye
PrintWriter pout = new PrintWriter (out, true);
p.out.println("Goodbye!");
// read that serialized Java object
ObjectInputStream oin = new ObjectInputStream(in);
Date date = (Date)oin.readObject();
client.close();
}
listener.close();
} catch (IOException e) {...}
catch (ClassNotFoundException e2) {...}
V. Summary
-
"When I started programming, we didn't have any of these sissy 'icons'
and 'Windows.'All we had were zeros and ones -- and sometimes we didn't
even have ones." "I wrote an entire database program using only zeros." "You
had zeros? We had to use the letter `O'."
-Dilbert (Scott Adams)
- You have a basic introduction to what's going on with networking.
- You learned about client/server architectures.
- You got an introduction to the code you need to communicate
between programs with Java.
page author: Joanna Bryson
21 February 2007