cs 10 problem solving via object oriented programming
play

CS 10: Problem solving via Object Oriented Programming - PowerPoint PPT Presentation

CS 10: Problem solving via Object Oriented Programming Winter 2017 Tim Pierson 260 (255) Sudikoff Day 22 Client/Server Agenda 1. Sockets 2.


  1. CS ¡10: ¡ Problem ¡solving ¡via ¡Object ¡Oriented ¡ Programming ¡ Winter ¡2017 ¡ ¡ Tim ¡Pierson ¡ 260 ¡(255) ¡Sudikoff ¡ Day ¡22 ¡– ¡Client/Server ¡

  2. Agenda ¡ 1. Sockets ¡ 2. Server ¡ 3. MulLthreaded ¡server ¡ 4. Chat ¡server ¡ 2 ¡

  3. Sockets ¡are ¡a ¡way ¡for ¡computers ¡to ¡ communicate ¡ • Client ¡1 ¡makes ¡ connecLon ¡over ¡ socket ¡ IP: ¡1.2.3.4 ¡ Client ¡1 ¡ HTTP ¡ • Server ¡receives ¡ Port: ¡80 ¡ connecLon, ¡moves ¡ communicaLons ¡ Server ¡ to ¡own ¡socket ¡ Server ¡is ¡listening ¡on ¡ a ¡socket ¡ (socket ¡= ¡IP ¡address ¡ ¡ ¡ ¡+ ¡protocol ¡ ¡ ¡ ¡+ ¡port) ¡ ¡ 3 ¡ Port ¡80 ¡= ¡HTTP ¡

  4. Sockets ¡are ¡a ¡way ¡for ¡computers ¡to ¡ communicate ¡ • Client ¡1 ¡makes ¡ connecLon ¡over ¡ socket ¡ IP: ¡1.2.3.4 ¡ ¡ Client ¡1 ¡ HTTP ¡ • Server ¡receives ¡ Port: ¡80 ¡ connecLon, ¡moves ¡ communicaLons ¡ Server ¡ to ¡own ¡socket ¡ Server ¡is ¡listening ¡on ¡ a ¡socket ¡ • Server ¡returns ¡to ¡ (socket ¡= ¡IP ¡address ¡ ¡ listening ¡ ¡ ¡+ ¡protocol ¡ ¡ ¡ ¡+ ¡port) ¡ • Server ¡talking ¡to ¡ ¡ Client ¡1 ¡and ¡ready ¡ 4 ¡ Port ¡80 ¡= ¡HTTP ¡ for ¡others ¡

  5. Sockets ¡are ¡a ¡way ¡for ¡computers ¡to ¡ communicate ¡ • Client ¡2 ¡makes ¡ connecLon ¡over ¡ socket ¡ IP: ¡1.2.3.4 ¡ Client ¡1 ¡ HTTP ¡ Port: ¡80 ¡ Server ¡ Server ¡is ¡listening ¡on ¡ a ¡socket ¡ (socket ¡= ¡IP ¡address ¡ ¡ Client ¡2 ¡ ¡ ¡+ ¡protocol ¡ ¡ ¡ ¡+ ¡port) ¡ ¡ 5 ¡ Port ¡80 ¡= ¡HTTP ¡

  6. Sockets ¡are ¡a ¡way ¡for ¡computers ¡to ¡ communicate ¡ • Client ¡2 ¡makes ¡ connecLon ¡over ¡ socket ¡ IP: ¡1.2.3.4 ¡ Client ¡1 ¡ HTTP ¡ • Server ¡receives ¡ Port: ¡80 ¡ connecLon, ¡moves ¡ communicaLons ¡ Server ¡ to ¡own ¡socket ¡ Server ¡is ¡listening ¡on ¡ a ¡socket ¡ • Server ¡returns ¡to ¡ (socket ¡= ¡IP ¡address ¡ ¡ listening ¡ Client ¡2 ¡ ¡ ¡+ ¡protocol ¡ ¡ ¡ ¡ ¡+ ¡port) ¡ • Server ¡talking ¡to ¡ ¡ client ¡1 ¡and ¡2 ¡ 6 ¡ Port ¡80 ¡= ¡HTTP ¡ ready ¡for ¡others ¡

  7. Java ¡provides ¡a ¡convenient ¡Socket ¡class ¡ WWWSocket.java ¡ • Run, ¡type ¡~tjp/cs10/index.php ¡ • Output ¡stream ¡= ¡from ¡your ¡computer ¡to ¡somewhere ¡else ¡ out.println ¡sends ¡data ¡to ¡another ¡computer ¡ • • Input ¡stream ¡= ¡from ¡another ¡computer ¡to ¡your ¡computer ¡ in.readLine ¡reads ¡data ¡sent ¡to ¡your ¡computer ¡ • 7 ¡

  8. Agenda ¡ 1. Sockets ¡ 2. Server ¡ 3. MulLthreaded ¡server ¡ 4. Chat ¡server ¡ 8 ¡

  9. We ¡can ¡create ¡our ¡own ¡server ¡ HelloServer.java ¡ public public static static void void main(String[] main(String[] args args) ) throws throws IOException IOException { { // Listen on a server socket for a connection System. out out.println .println("waiting for someone to connect" "waiting for someone to connect"); ); ServerSocket listen = new new ServerSocket ServerSocket(4242); (4242); // When someone connects, create a specific socket for them IP: ¡localhost ¡ Socket sock = listen.accept(); System. out out.println .println("someone connected" "someone connected"); ); TCP ¡ Port: ¡4242 ¡ // Now talk with them PrintWriter out = new new PrintWriter PrintWriter(sock sock.getOutputStream .getOutputStream(), (), true true); ); BufferedReader in = new new BufferedReader BufferedReader(new new InputStreamReader InputStreamReader(sock sock.getInputStream .getInputStream())); ())); out.println("who is it?"); String line; Server ¡ while while (( ((line line = = in in.readLine .readLine()) != ()) != null null) { ) { System. out out.println .println("received:" "received:" + + line line); ); out.println("hi " + line + "! anybody else there?"); } System. out out.println .println("client hung up" "client hung up"); ); // Clean up shop out.close(); in.close(); sock.close(); listen.close(); } ¡ Run, ¡then ¡from ¡terminal ¡type ¡telnet ¡localhost ¡4242 ¡ 9 ¡

  10. We ¡can ¡also ¡create ¡our ¡own ¡client ¡too ¡ HelloServer.java ¡and ¡HelloClient.java ¡ HelloServer.java ¡ 10 ¡

  11. We ¡can ¡create ¡our ¡own ¡client ¡too ¡ HelloClient.java ¡ public public class class HelloClient HelloClient { { public public static static void void main(String[] main(String[] args args) ) throws throws Exception { Exception { Scanner console = new new Scanner( Scanner(System. System. in in); ); // Open the socket with the server, and then the writer and reader System. out out.println .println("connecting..." "connecting..."); ); Socket sock = new new Socket( Socket("localhost" "localhost",4242); ,4242); //new Socket("129.170.212.159", 4242); //new Socket("129.170.212.159", 4242); PrintWriter out = new new PrintWriter PrintWriter(sock sock.getOutputStream .getOutputStream(), (), true true); ); BufferedReader in = new new BufferedReader BufferedReader(new new InputStreamReader InputStreamReader(sock sock.getInputStream .getInputStream())); ())); System. out out.println .println("...connected" "...connected"); ); // Now listen and respond Client ¡ ¡ String line; while while (( ((line line = = in in.readLine .readLine()) != ()) != null null) { ) { // Output what you read System. out out.println .println(line line); ); // Get some more input (from the user) to write to the open socket (server) String name = console.nextLine(); out.println(name); } System. out out.println .println("server hung up" "server hung up"); ); // Clean up shop out.close(); in.close(); sock.close(); } } ¡ Run ¡HelloServer.java ¡ Then ¡run ¡HelloClient.java ¡ ¡ 11 ¡

  12. Friends ¡can ¡connect ¡to ¡your ¡server ¡if ¡they ¡ connect ¡to ¡the ¡right ¡IP ¡address ¡ Run ¡MyIPAdressHelper.java ¡to ¡get ¡your ¡address, ¡edit ¡HelloClient.java ¡ ¡ 12 ¡

  13. ConnecLng ¡from ¡another ¡machine ¡ HelloServer.java ¡and ¡HelloClient.java ¡ • Run ¡MyIPAddressHelper ¡on ¡server ¡to ¡get ¡IP ¡ • Start ¡HelloClient.java ¡on ¡server ¡ • Edit ¡HelloClient.java ¡to ¡change ¡localhost ¡to ¡server ¡IP ¡address ¡ • Run ¡HelloClient ¡on ¡client ¡machines ¡and ¡make ¡connecLon ¡ ¡ • Connect ¡from ¡student ¡machine? ¡ 13 ¡

  14. Agenda ¡ 1. Sockets ¡ 2. Server ¡ 3. MulLthreaded ¡server ¡ 4. Chat ¡server ¡ 14 ¡

  15. Currently ¡our ¡server ¡can ¡only ¡handle ¡one ¡ client ¡at ¡a ¡Lme ¡ Using ¡Java’s ¡Thread ¡mechanism ¡to ¡overcome ¡single ¡client ¡issue ¡ • We ¡would ¡like ¡our ¡server ¡to ¡talk ¡to ¡mulLple ¡clients ¡at ¡a ¡ Lme ¡ • Trick ¡is ¡to ¡give ¡each ¡client ¡its ¡own ¡socket ¡ • That ¡way ¡the ¡server ¡can ¡talk ¡“concurrently” ¡with ¡mulLple ¡ clients ¡ • Java ¡provides ¡a ¡Thread ¡class ¡to ¡handle ¡concurrency ¡ (mulLple ¡processes ¡running ¡at ¡same ¡Lme) ¡ • Threads ¡are ¡much ¡lighter ¡than ¡running ¡mulLple ¡instances ¡ of ¡a ¡program ¡(more ¡on ¡threads ¡next ¡class) ¡ • Inherit ¡from ¡Thread ¡class ¡and ¡override ¡ run ¡ method ¡ • Start ¡thread ¡using ¡ start ¡method ¡ 15 ¡

  16. We ¡can ¡create ¡a ¡“Communicator” ¡on ¡a ¡ separate ¡thread ¡for ¡each ¡connecLon ¡ One ¡Communicator ¡allocated ¡for ¡a ¡single ¡client ¡ 16 ¡

  17. We ¡can ¡create ¡a ¡“Communicator” ¡on ¡a ¡ separate ¡thread ¡for ¡each ¡connecLon ¡ MulGple ¡Communicators ¡allocated ¡for ¡mulGple ¡clients ¡ 17 ¡

  18. We ¡can ¡create ¡a ¡“Communicator” ¡on ¡a ¡ separate ¡thread ¡for ¡each ¡connecLon ¡ HelloMulGthreadedServer.java ¡ • Starts ¡new ¡thread ¡with ¡new ¡HelloServerCommunicator ¡on ¡each ¡ connecLon ¡ HelloServerCommunicator.java ¡ • Extends ¡Thread ¡ • Override ¡run ¡ • Tracks ¡thread ¡ID ¡ • Otherwise ¡the ¡same ¡as ¡single ¡threaded ¡version ¡ ¡ Run ¡HelloMulGthreadedServer.java ¡with ¡mulGple ¡telnets ¡ 18 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend