Java-Networking
Download
Report
Transcript Java-Networking
Java Networking
•Motivation
•Network Layers
•Using Sockets
•A Tiny Server
•Applets
•URLs
•Downloading Images, MediaTracker
CSE 341, S. Tanimoto
Java networking-
1
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-
2
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-
3
Server
Sockets
Client
int port=2000;
BufferedReader br;
PrintWriter pw;
ServerSocket ss;
Socket sock;
int port=2000;
String host=”cubist";
BufferedReader br;
PrintWriter pw;
Socket sock;
ss = new ServerSocket(port);
sock = ss.accept();
br = new BufferedReader(
new InputStreamReader(
(sock.getInputStream());
pw = new PrintWriter(
sock.getOutputStream());
sock = new Socket(host,port);
br = new BufferedReader(
new InputStreamReader(
(sock.getInputStream());
pw = new PrintWriter(
sock.getOutputStream());
System.out.println(
br.readLine());
pw.println("HELLO");
pw.println("ciao");
System.out.println(
br.readLine());
CSE 341, S. Tanimoto
Java networking-
4
A Tiny Server
import java.net.*; import java.io.*;
public
static
static
static
class TestServer {
int port=2000;
static BufferedReader br;
PrintWriter pw;
static ServerSocket ss;
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-
5
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-
6
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-
7
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-
8
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-
9