Presentation is loading. Please wait.

Presentation is loading. Please wait.

Practicum: - Client-Server Computing in Java 15-211 Fundamental Data Structures and Algorithms Peter Lee April 8, 2004.

Similar presentations


Presentation on theme: "Practicum: - Client-Server Computing in Java 15-211 Fundamental Data Structures and Algorithms Peter Lee April 8, 2004."— Presentation transcript:

1 Practicum: - Client-Server Computing in Java 15-211 Fundamental Data Structures and Algorithms Peter Lee April 8, 2004

2 Reminders  HW7 is out!  due on Wednesday, April 28, 11:59pm  Read:  Chapter 10  Next time:  Guest lecture by Andreas Nowatzyk (Deep Thought)

3 Recap: Learning to Play Games

4 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

5 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

6 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

7 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

8 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

9 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

10 State-of-the-art: Backgammon  Gerald Tesauro (IBM)  Wrote a program which became “overnight” the best player in the world  Not easy!

11 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 5 97 8 2 ab c d

12 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

13 Today’s Topic: Intro to Distributed Computing Concepts

14 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 …

15 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

16 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

17 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

18 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

19 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

20 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)

21 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()

22 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

23 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

24 Client-server interaction listenSocket = ServerSocket(port) connectionSocket = listenSocket.accept() read request(s) from connectionSocket write reply(s) to connectionSocket connectionSocket.close() clientSocket = Socket(hostid,port) send request(s) to clientSocket read reply(s) from clientSocket clientSocket.close() Server Client

25 Example: A Pig Latin Server

26 Example: Java client import java.io.*; import java.net.* public class Client { public static void main (String argv[]) throws Exception { BufferedReader user = new BufferedReader(…); Socket clientSocket = new Socket(“foo.cs.cmu.edu”, 6789); PrintWriter out = new PrintWriter( clientSocket.getOutputStream(),true); … create user input stream connect to the server create output stream

27 Java client, cont’d … 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(); create input stream send request to the server read reply from server release the connection

28 Example: Java server import java.io.*; import java.net.* public class Server { public static void main (String argv[]) throws Exception { ServerSocket listenSocket = new ServerSocket(6789); while (true) { Socket connectionSocket = listenSocket.accept(); BufferedReader in = new BufferedReader( new InputStreamReader( connectionSocket.getInputStream())); create listening socket wait for client contact create input stream on host foo.cs.cmu.edu:

29 Java server, cont’d PrintWriter out = new PrintWriter( connectionSocket.getOutputStream(),true); clientSentence = in.readLine(); String pigLatin = pigTranslate(clientSentence); out.println(pigLatin); connectionSocket.close(); } create output stream read request from client service the request send reply to client end of while loop; go back and wait for another request

30 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

31 Exception handling … 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) { … } } this is always executed, no matter what

32 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

33 Example: multithreaded server import java.io.*; import java.net.* public class Server extends Thread { ServerSocket listenSocket; public Server () { try { listenSocket = new ServerSocket(6789); } catch (IOException e) { … } this.start(); } … create listening socket when client contacts us, start a server loop thread for it on host foo.cs.cmu.edu:

34 Multithreaded server, cont’d … public void run() { try { while (true) { Socket connectionSocket = listenSocket.accept(); … run() is invoked by start(), after a new thread is created


Download ppt "Practicum: - Client-Server Computing in Java 15-211 Fundamental Data Structures and Algorithms Peter Lee April 8, 2004."

Similar presentations


Ads by Google