Documente Academic
Documente Profesional
Documente Cultură
8 Networking Networking.
!Communicate between different devices.
!Client-server.
!Peer-to-peer.
Networking in Java.
If you have a wireless laptop, download the following programs: !Mail spoofing.
http://www.cs.princeton.edu/introcs/84network/ChatClient.java
!Echo.
http://www.cs.princeton.edu/introcs/84network/In.java !Remote control.
http://www.cs.princeton.edu/introcs/84network/Out.java !Chatroom.
Detours.
! Graphical user interface.
! Threads.
Introducti on to Computer Sci ence Robert Sedgew i ck and Kevi n Wayne http://w w w .cs.Pri nceton.EDU/IntroCS 2
3 4
TCP/IP Protocols
Internet protocol. (IP) Many higher layer application protocols use TCP/IP.
!Rules for moving bits from A to B. !Used by http, smtp, telnet, ftp.
!Divide data into packets (header bits to say where to go, data bits)
and transmit each individually, possibly along different paths. Ex 1: HyperText Transfer Protocol (HTTP).
!No guarantee packets arrive in order, or even arrive. ! Set of rules for transferring files (text, graphics, video).
! Server java.sun.com waits for connection requests.
Transmission control protocol. (TCP) ! Browser establishes connection with server at http://java.sun.com.
!Rules to provide reliable communication between two computers. ! Browser issues GET and POST commands.
tells system to use http protocol to
!Packets arrive, and they arrive in order. ! Server responds with header and body.
get the specified resource
resend over IP until recipient acknowledges
Ex 2: Simple Mail Transfer Protocol (SMTP). A Java program that uses SMTP.
!Set of rules for sending email.
import java.net.Socket;
!Server smtp.princeton.edu waits for connection requests on port 25. public class Mail {
!User specifies sender, recipient, subject, message body, etc. public static void main(String[] args) throws Exception { open socket
Socket socket = new Socket("smtp.princeton.edu", 25); open socket
In in = new In(socket);
phoenix.Princeton.EDU% telnet smtp.princeton.edu 25
Out out = new Out(socket); mail server port
220 smtpserver1.Princeton.EDU ESMTP Sendmail 8.12.9/8.12.9;
Sun, 7 Dec 2003 09:18:47 -0500 (EST) out.println("HELO localhost"); SMTP protocol
HELO localhost System.out.println(in.readLine());
250 smtpserver1.Princeton.EDU Hello phoenix.Princeton.EDU out.println("MAIL From: AlanTuring@cnn.com"); forged address
[128.112.128.42], pleased to meet you
Warni ng: do not spoof w i thout System.out.println(in.readLine()); read from socket
MAIL From: AlanTuring@cnn.com
250 2.1.0 AlanTuring@cnn.com... Sender ok recei ver's consent out.println("RCPT To: wayne@cs.princeton.edu"); write to socket
RCPT TO: wayne@cs.princeton.edu System.out.println(in.readLine());
250 2.1.5 wayne@cs.princeton.edu... Recipient ok out.println("DATA");
DATA out.println("Subject: Just wanted to say hi");
354 Enter mail, end with "." on a line by itself out.println("Hope you're enjoying COS 126.");
Subject: Just wanted to say hi out.println(".");
Hope you're enjoying COS 126.
System.out.println(in.readLine());
.
250 2.0.0 hB7EIlgn024881 Message accepted for delivery
quit out.close(); close socket
in.close();
socket.close();
}
Open relay. smtp server accessible from anywhere. }
7 8
Client-Server Echo Client
Stream socket. Echo client: connect with server, read text from standard input, send
! ADT for two-way communication over TCP/IP. text to server, print whatever server sends back.
read from socket input and write to socket output
import java.net.Socket;
! IP address: identifies computer. public class EchoClient {
! Port number: identifies application. public static void main(String[] args) throws Exception {
! Ex: IP address = 128.112.129.71, Port = 25. Socket socket = new Socket(args[0], 4444); open socket
In stdin = new In();
smtp.princeton.edu smtp In in = new In(socket); server name
mail server email application Out out = new Out(socket);
! Server = creates one socket to listen for connection requests; out.close(); close socket
in.close();
creates another socket to communicate with each client. socket.close();
! Ex: client = web browser, server = web server. }
}
9 10
Echo server: use ServerSocket to listen for connection requests; Remote control server.
connect with a client; read text that client sends; and send it back. !Client sends text commands.
!Server launches: { TiVo recorder, coffee maker, burglar alarm }.
public class EchoServer { !Server acknowledges task completion.
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(4444);
open server socket Trivial modification to Java programs.
while (true) { and listen on port 4444
! Detail: must be running Java coffee maker on Internet.
Socket socket = serverSocket.accept();
In in = new In (clientSocket); listen and wait for
Out out = new Out(clientSocket); client to request
connection
String s;
read data from client
while ((s = in.readLine()) != null)
and echo it back
out.println(s);
out.close();
close input streams
in.close();
and socket
socket.close();
}
}
}
11 12
Multi-Threaded Chatroom Application Graphical User Interface
Callback.
! Programmer registers an event to listen for.
! Programmer writes code to process event.
13 14
15 16
Java Multi-Threaded Server: ChatServer Listener Thread: Broadcast Messages to Clients
17 18
Gotcha: two threads simultaneously accessing the same object. Server maintains a separate thread for each client connection.
! ith connection thread puts next message into client i's buffer. public class Client extends Thread {
! listener thread gets next message from client i's buffer. private String buffer; message received from client
...
public Client(Socket socket) { ... }
public void run() { only this thread gets held up if no incoming message
Dining philosophers problem. String s;
! To eat, philosopher needs both adjacent chopsticks. while ((s = in.readLine()) != null) { setMessage(s); }
}
! If each philosopher grabs chopstick to right, then
waits for chopstick to left, then everyone starves. public synchronized String getMessage() { executes in
if (buffer == null) return null; one thread
! Deadlocking = bane of programming with threads. String temp = buffer;
buffer = null;
notifyAll(); tell setMessage to stop waiting
Java has built-in support for synchronization. return temp;
}
! Indicate blocks of code that can't be simultaneously accessed.
! No need unless you are using more than one thread. public synchronized void setMessage(String s) { executes in
if (buffer != null) another thread
wait();
buffer = s; if current message not yet broadcast,
} then wait to receive next message
}
19 20
Peer-to-Peer Networking Peer-to-Peer Networking
!User queries server to find file, the connects directly with host. No central point of control or failure.
!
Scalable.
Z
!
Y Napster Server Farm o ...?
Wh
...? s file
ho Who ha
W X does! A? X
I have song A
.?
W ho ..
Wh
Transfer o ...?
song A
Who has song A? Wh
oh
a
Check Yand Z A? s file
Y
X
Reference: http://di gi tal5.ece.tntech.edu/hexb/690f03/lectures.htm Reference: http://di gi tal5.ece.tntech.edu/hexb/690f03/lectures.htm
21 22
Summary
Client-server vs peer-to-peer.
! Client-server for browsing web.
! Peer-to-peer for file-sharing.
23