LectureClientServerP..

Download Report

Transcript LectureClientServerP..

Practicum:
- Client-Server Computing in Java
15-211
Fundamental Data Structures and
Algorithms
Peter Lee
April 8, 2004
Reminders
 HW7 is out!
due on Wednesday, April 28, 11:59pm
 Read:
Chapter 10
 Next time:
Guest lecture by Andreas Nowatzyk
(Deep Thought)
Recap:
Learning to Play Games
Game playing strategies
 Last time we were introduced to
minimax search as an approach to
two-player games
 For interesting games, it is very
important to prune the search tree
 We saw the alpha/beta pruning
technique could be very useful
AI
 But the quality of the evaluation
function and the search order is also
of critical importance
 So this leaves us with the question:
How do we come up with a good
evaluation function and a good
search order?
These are fundamental questions in
Artificial Intelligence
Learning
 We can, of course, be as thoughtful
and clever as possible, analyze the
game carefully, and design an
evaluation function accordingly
 But this is often difficult
 Another approach is to arrange for
our game playing program to learn
Many approaches…
 Indeed, machine learning is a very
big and important topic in CS
take 15-381, 15-681, etc
 many approaches, some of them
highly mathematical or statistical
Samuels checkers program
 An early 60’s checkers program by
Samuels learned by playing games
against good human players
 It updated its evaluation function
whenever it won a game, and
ignored losing games
 In time, it became the best checkers
player in major tournaments
Learning from humans or computers?
 While Samuel’s program was very
successful, it also had some
drawbacks
It would actually get worse when
playing poor players
Humans play very slowly, and so this
limited the pace of learning
State-of-the-art: Backgammon
 Gerald Tesauro (IBM)
 Wrote a program which became
“overnight” the best player in the
world
 Not easy!
State-of-the-art: Backgammon
 Learned the evaluation function by playing
1,500,000 games against itself
 Temporal credit assignment using
reinforcement learning
 Used Neural Network to learn the evaluation
function
c
8
a
9
d
2
5
b
7
State-of-the-art: Go
 Average branching factor 360
 Regular search methods go bust !
 People use higher level strategies
 Systems use vast knowledge bases
of rules… some hope, but still play
poorly
 $2,000,000 for first program to
defeat a top-level player
Today’s Topic:
Intro to
Distributed Computing
Concepts
Distributed computing
 Many applications involve
coordinated computation by multiple
host computers
World-Wide Web
Internet Chess Club
Andrew File System
E-Mail services
X Windows
…
The client-server paradigm
 Almost all modern distributed computing
applications are organized around the
client-server paradigm
 Client
 initiates communication
 sends requests and receives responses
 interacts with one (or a small number) of
servers at a time
 Server
 waits and listens for incoming requests
 receives requests and sends responses
 interacts with many clients concurrently
Network communication
 Network communication is organized into
layers
 hardware layer
 network interface device, connected to a local area
network, which in turn is connected to a (packet
switched) Internet
 protocol layer(s)
 basic data transport mechanisms, providing
addressing (eg, IP addresses),
fragmention/reassembly, reliable transmission
 application layer
 client and server functionality
Hardware layer
 Typically Ethernet-based
 Data is transmitted in small packets
 typically less than 1500 bytes
 packets are easily lost, and often arrive in
unpredictable order
 Each packet contains routing information
 A network device watches for packets that
are addressed to itself, ignores the rest
 Routers look for packets that are not
addressed to local hosts, and forwards
them to a non-local network router
Protocol layer
 There are two main protocols used
 both provide Internet addressing/routing
 TCP
 Transmission Control Protocol
 connection (“session”) oriented
 provides long data messages (via
fragmentation and reassembly of packets)
 provides reliable communication
 UDP
 Unreliable Datagram Protocol
 not connection oriented
 no transmission guarantees, but very
lightweight and efficient
Application layer
 The hardware and protocol layers are
studied in 15-441
 Here, we will focus on the application level
 Most networking applications use a
particular data structure, the socket
 A socket provides a high-level abstract
interface to a lower-level network protocol
service
 In Java, sockets are similar in some respects
to input/output streams
Sockets
 Sockets have a long history in network
and operating system design
 first appeared in BSD 4.1 Unix in 1981
 Socket characteristics
 applications explicitly create, use, and destroy
sockets
 distinction between client sockets and server
sockets
 different kinds of sockets, depending on the
transport protocol (TCP vs UDP)
Sockets in Java
 Java provides very good support for sockets
in the java.net.* package
 java.net.Socket
 create: constructor methods (to create sockets)
 I/O: getOutputStream(), getInputStream()
 destroy: close()
 java.net.ServerSocket
 create: constructor methods
 wait and listen: accept()
 destroy: close()
Socket programming with TCP
 Client must contact the server
server must first be waiting and
listening
server must thus have created a socket
that accepts client connection request
 Client contacts server by:
creating its own client TCP socket
 uses IP address and port number of server
Socket programming, cont’d
 When client creates its socket, a TCP
session with the server’s TCP is
established
 On the server side:
when contacted by the client, the server
TCP creates a new socket for
communication with the client
thus, each client session gets its own
private socket on the server
Client-server interaction
Server
Client
listenSocket =
ServerSocket(port)
connectionSocket =
listenSocket.accept()
read request(s) from
connectionSocket
write reply(s) to
connectionSocket
clientSocket =
Socket(hostid,port)
send request(s) to
clientSocket
read reply(s) from
clientSocket
connectionSocket.close()
clientSocket.close()
Example:
A Pig Latin Server
Example: Java client
import java.io.*;
import java.net.*
create user
input stream
connect to
the server
public class Client {
public static void main (String argv[])
throws Exception {
BufferedReader user =
new BufferedReader(…);
Socket clientSocket =
new Socket(“foo.cs.cmu.edu”, 6789);
create output
stream
PrintWriter out = new PrintWriter(
clientSocket.getOutputStream(),true);
…
Java client, cont’d
create input
stream
send request
to the server
read reply
from server
release the
connection
…
BufferedReader in =
new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
String sentence = user.readLine();
out.println(sentence);
String pigLatin = in.readLine();
System.out.println(“Server says:” +
pigLatin);
clientSocket.close();
Example: Java server
on host foo.cs.cmu.edu:
import java.io.*;
import java.net.*
public class Server {
create listening public static void main (String argv[])
throws Exception {
socket
ServerSocket listenSocket =
new ServerSocket(6789);
wait for
client contact
while (true) {
Socket connectionSocket =
create input
listenSocket.accept();
stream
BufferedReader in =
new BufferedReader(
new InputStreamReader(
connectionSocket.getInputStream()));
Java server, cont’d
create output
stream
PrintWriter out =
new PrintWriter(
read request
connectionSocket.getOutputStream(),true);
from client
service the
request
clientSentence = in.readLine();
String pigLatin =
pigTranslate(clientSentence);
send reply to
client
out.println(pigLatin);
connectionSocket.close();
}
end of while loop; go back and
wait for another request
Practical issue: cleaning up
 Closing connections
It is important to close connections
 usually a strict limit on the number of open
connections
This means it is very important to
handle exceptions, in case the socket
creation or I/O fail
 exception handler should close any open
connections
Exception handling
this is always
executed, no
matter what
…
try {
Socket clientSocket =
new Socket(“foo.cs.cmu.edu”, 6789);
…
} catch (UnknownHostException e) {
System.err.println(
“Couldn’t find the server host!”);
} catch (IOException e) {
System.err.println(
“I/O error!”);
} finally {
try {
if (clientSocket != null) {
out.close();
in.close();
clientSocket.close();
} } catch (IOException e) { … }
}
Practical issue: concurrency
 The server usually must be designed
to handle multiple clients
concurrently
 This means that the server should be
set up so that the multiple copies of
the main server loop can be running
at the same time
 Java provides a mechanism for such
separate “threads” of control, in
java.lang.Thread
Example: multithreaded server
on host foo.cs.cmu.edu:
import java.io.*;
import java.net.*
create listening
socket
public class Server extends Thread {
ServerSocket listenSocket;
public Server () {
try {
listenSocket =
new ServerSocket(6789);
} catch (IOException e) { … }
when client
contacts us,
start a server
loop thread
for it
this.start();
}
…
Multithreaded server, cont’d
run() is invoked
by start(), after a
new thread is
created
…
public void run() {
try {
while (true) {
Socket connectionSocket =
listenSocket.accept();
…