Java Networking

Download Report

Transcript Java Networking

Java Networking
•Motivation
•Sockets
•Applets
•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 an Applets
•Anticipate bandwidth limitations
CSE 341, S. Tanimoto
Java networking-
2
Server
Sockets
Client
int port=2000;
BufferedReader br;
PrintWriter pw;
ServerSocket ss;
Socket sock;
int port=2000;
String host=“piaget”;
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-
3
A Tiny Server
import java.net.*;
public
static
static
static
import java.io.*;
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-
4
Applets
•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-
5
Downloading Images
•Java anticipated the download times required
to retrieve images.
•provides the MediaTracker method 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-
6