Unformatted text preview:

October 1, 2009 CS 350 Lecture 11 1CS 350: Computer/Human InteractionLecture 11 Overview●Basic networking●Network applications●Java sockets●References: JTUT●Assignments out: Java prototypeOctober 1, 2009 CS 350 Lecture 11 2CS 350: Computer/Human InteractionBasic Networking●Hosts have domain names (e.g., csserver.evansville.edu) and IP addresses (e.g., 10.10.0.9 – internal; 192.195.228.35 – external)●Communication between hosts is through a port. Low-numbered ports are reserved for and assigned to well-known services. E.g., sshd listens on port 22. (List is in /etc/services on Unixes.)October 1, 2009 CS 350 Lecture 11 3CS 350: Computer/Human InteractionNetwork Applications●Network applications usually are structured in a client-server architecture using TCP/IP, a reliable, connection-oriented protocol.●Servers listen on (assigned) ports. ●Client uses ephemeral or short-lived ports, usually assigned automatically by TCP.●Often multiple clients talk to single server.October 1, 2009 CS 350 Lecture 11 4CS 350: Computer/Human InteractionIn-class Exercise 1●Download PizzaJava4-5.zip from course website.●Unzip into the Java Eclipse workspace; 3 more projects●Launch Eclipse, add Pizza4App, Pizza5Applet, and PizzaServer projectsOctober 1, 2009 CS 350 Lecture 11 5CS 350: Computer/Human InteractionClient-Server Applications●Pizza4App is a network client of a server (PizzaServer) that receives order data and writes it to the server’s file system.●Client is same as Version 1 with some Version 2 features.●Server consists of main server class and a handler thread class.October 1, 2009 CS 350 Lecture 11 6CS 350: Computer/Human InteractionSockets●Look at PizzaServer.java●A socket is an (IP address, port) pair that uniquely identifies an endpoint of a connection.●A server will create a listening socket and wait to accept client connections. When a connection is received and accepted, the server typically will fork off a thread to handle the connection via a connected socket.October 1, 2009 CS 350 Lecture 11 7CS 350: Computer/Human InteractionJava Sockets●Two types of sockets–ServerSocket: a listening socket; constructor receives port number, value of 0 has system choose a free port number–Socket: a connected socket; constructor receives host domain name and port number; also the handler socket created by calling accept method of ServerSocketServerSocket listenSocket = new ServerSocket(0);Socket connectSocket = listenSocket.accept();October 1, 2009 CS 350 Lecture 11 8CS 350: Computer/Human InteractionServer Threads●Look at PizzaServerThread.java●PPP server creates a PizzaServerThread object to handle each connection. The count argument is just a way to distinguish threads.●PizzaServerThread is a class that extends the Thread class that implements the Runnable interface. Must implement run() method that is called automatically when the thread is started.October 1, 2009 CS 350 Lecture 11 9CS 350: Computer/Human InteractionSocket I/O●Wrap socket streams into buffered streams to read and write data.BufferedReader in;in = new BufferedReader( new InputStreamReader( socket.getInputStream()));String s = in.readLine();PrintWriter out;out = new PrintWriter( socket.getOutputStream(), true);out.println(s);October 1, 2009 CS 350 Lecture 11 10CS 350: Computer/Human InteractionMessage Protocol●Look at Pizza4App.java●Client and server must agree on the format of messages between them.●PPP transmits/receives data in lines in order of the form. Address field may have more than one line and may have more than one topping, so transmission of those data have extra end data markers.October 1, 2009 CS 350 Lecture 11 11CS 350: Computer/Human InteractionIn-class Exercise 2●Log into csserver. Change directories to webspace. Do command:$ tar -xvf /home/hwang/cs350/PizzaServer.tar●Compile server: $ javac PizzaServer.java●Run server: $ java PizzaServer●Port number put in file PizzaPort.txt●Create output directory: $ mkdir output$ chmod 703 outputOctober 1, 2009 CS 350 Lecture 11 12CS 350: Computer/Human InteractionIn-class Exercise 2●Run Pizza4App●Browse to PizzaPort.txt●Enter host (csserver.evansville.edu) and port number to connect●DEBUG is set to true, so console output is shown to help see what is happening. Recompile with DEBUG set to false to get rid of the output.October 1, 2009 CS 350 Lecture 11 13CS 350: Computer/Human InteractionNotes●PPP client-server code is not as robust as it could be. It is possible to get into situations where the client locks up waiting for something.●Handler threads automatically exit when the client disconnects, but the server process will execute indefinitely if put in the background.October 1, 2009 CS 350 Lecture 11 14CS 350: Computer/Human InteractionNotes●This example is given primarily for completeness and is not required. In particular, applets cannot write to the local machine, so they must talk to a server to save information. ●Look at Pizza5Applet.java●Compile Pizza5Applet on csserver●Browse to pizza5applet.html to run itOctober 1, 2009 CS 350 Lecture 11 15CS 350: Computer/Human InteractionIn-class Exercise 3●Modify any version of the Purple Pizza Parlor Order form–Add sizes and/or toppings. Separate the toppings into lists of meats and vegetables.–Add a phone field, add error checking to make sure it’s filled in, add code to write the data to the output file–Add pricing information and display the computed total in a dialog


View Full Document

UE CS 350 - LECTURE NOTES

Download LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?