100 likes | 219 Views
Java Networking. Motivation Network Layers Using Sockets A Tiny Server Applets URLs Downloading Images, MediaTracker. Motivation. Support distributed model of computation Permit programs to download documents Permit programs to run as Applets Anticipate bandwidth limitations.
E N D
Java Networking • Motivation • Network Layers • Using Sockets • A Tiny Server • Applets • URLs • Downloading Images, MediaTracker CSE 341, S. Tanimoto Java networking-
Motivation • Support distributed model of computation • Permit programs to download documents • Permit programs to run as Applets • Anticipate bandwidth limitations CSE 341, S. Tanimoto Java networking-
Network Layers • Application Level • (FTP, Telnet, etc.) • Transport Layer • (TCP, UDP, sockets, etc.) • Network Layer • (Low-level Protocol -- IP, datagrams, etc.) • Hardware Layer • (Ethernet, TokenRing, X.25, etc.) CSE 341, S. Tanimoto Java networking-
Sockets Server Client int port=2000; BufferedReader br; PrintWriter pw; ServerSocket ss; Socket sock; ss = new ServerSocket(port); sock = ss.accept(); br = new BufferedReader( new InputStreamReader( (sock.getInputStream()); pw = new PrintWriter( sock.getOutputStream()); System.out.println( br.readLine()); pw.println("HELLO"); int port=2000; String host=”cubist"; BufferedReader br; PrintWriter pw; Socket sock; sock = new Socket(host,port); br = new BufferedReader( new InputStreamReader( (sock.getInputStream()); pw = new PrintWriter( sock.getOutputStream()); pw.println("ciao"); System.out.println( br.readLine()); CSE 341, S. Tanimoto Java networking-
A Tiny Server import java.net.*; import java.io.*; public class TestServer { static int port=2000; static BufferedReader br; static PrintWriter pw; static ServerSocket ss; static Socket sock; public static void main(String [] args) { try { ss = new ServerSocket(port); sock = ss.accept(); br = new BufferedReader(new InputStreamReader( (sock.getInputStream()))); pw = new PrintWriter(sock.getOutputStream()); System.out.println(br.readLine()); pw.println("HELLO"); } catch (IOException e) {System.out.println("Error connecting to port");} } } CSE 341, S. Tanimoto Java networking-
Applets • Developed to fit a W W W client/server architecture with fat clients • Normally applets live “in the browser” • Limited permissions (talk only to the hosting server, don’t touch local hard disk). • Use applet context as an operating env’t. CSE 341, S. Tanimoto Java networking-
URLs • Uniform Resource Locator • http://cubist.cs.washington.edu/~john/d.lsp?q=hello • ftp://ftp.cs.washington.edu/pub/myprog.tar • class URL • URL u; String mydoc; • try { • u = new URL("http://www.dom.com/x.html"); • } • catch (MalformedURLException e) { • system.out.println("MalformedURLException for " + • filename); return; • } • try { • mydoc = (String) u.getContent(); • } • catch (IOException e) {} CSE 341, S. Tanimoto Java networking-
Downloading Images • Java anticipated the download times required to retrieve images. • provides the MediaTracker class to keep track of the status of files that are being downloaded. • Images can be retrieved with URLs that point to either JPEG or GIF images. CSE 341, S. Tanimoto Java networking-
Other Networking Issues • Distributed objects: Java serialization and class loaders • Security: levels of trust/privileges, signed applets • Robustness: Avoiding errors, handling errors • Practical distribution of code and data: JAR files • Reverse engineering and Intellectual Property protection CSE 341, S. Tanimoto Java networking-