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

cs 10 problem solving via object oriented programming
SMART_READER_LITE
LIVE PREVIEW

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.


slide-1
SLIDE 1

CS ¡10: ¡ Problem ¡solving ¡via ¡Object ¡Oriented ¡ Programming ¡

Winter ¡2017 ¡

¡

Tim ¡Pierson ¡

260 ¡(255) ¡Sudikoff ¡

Day ¡22 ¡– ¡Client/Server ¡

slide-2
SLIDE 2

2 ¡

Agenda ¡

  • 1. Sockets ¡
  • 2. Server ¡
  • 3. MulLthreaded ¡server ¡
  • 4. Chat ¡server ¡
slide-3
SLIDE 3

3 ¡

Sockets ¡are ¡a ¡way ¡for ¡computers ¡to ¡ communicate ¡

Server ¡ Client ¡1 ¡ Server ¡is ¡listening ¡on ¡ a ¡socket ¡ (socket ¡= ¡IP ¡address ¡ ¡ ¡ ¡+ ¡protocol ¡ ¡ ¡ ¡+ ¡port) ¡ ¡ Port ¡80 ¡= ¡HTTP ¡

  • Client ¡1 ¡makes ¡

connecLon ¡over ¡ socket ¡

  • Server ¡receives ¡

connecLon, ¡moves ¡ communicaLons ¡ to ¡own ¡socket ¡

IP: ¡1.2.3.4 ¡ HTTP ¡ Port: ¡80 ¡

slide-4
SLIDE 4

4 ¡

Sockets ¡are ¡a ¡way ¡for ¡computers ¡to ¡ communicate ¡

Server ¡ Server ¡is ¡listening ¡on ¡ a ¡socket ¡ (socket ¡= ¡IP ¡address ¡ ¡ ¡ ¡+ ¡protocol ¡ ¡ ¡ ¡+ ¡port) ¡ ¡ Port ¡80 ¡= ¡HTTP ¡

  • Client ¡1 ¡makes ¡

connecLon ¡over ¡ socket ¡ ¡

  • Server ¡receives ¡

connecLon, ¡moves ¡ communicaLons ¡ to ¡own ¡socket ¡

  • Server ¡returns ¡to ¡

listening ¡

  • Server ¡talking ¡to ¡

Client ¡1 ¡and ¡ready ¡ for ¡others ¡ Client ¡1 ¡

IP: ¡1.2.3.4 ¡ HTTP ¡ Port: ¡80 ¡

slide-5
SLIDE 5

5 ¡

Sockets ¡are ¡a ¡way ¡for ¡computers ¡to ¡ communicate ¡

Server ¡ Server ¡is ¡listening ¡on ¡ a ¡socket ¡ (socket ¡= ¡IP ¡address ¡ ¡ ¡ ¡+ ¡protocol ¡ ¡ ¡ ¡+ ¡port) ¡ ¡ Port ¡80 ¡= ¡HTTP ¡

  • Client ¡2 ¡makes ¡

connecLon ¡over ¡ socket ¡ Client ¡2 ¡ Client ¡1 ¡

IP: ¡1.2.3.4 ¡ HTTP ¡ Port: ¡80 ¡

slide-6
SLIDE 6

6 ¡

Sockets ¡are ¡a ¡way ¡for ¡computers ¡to ¡ communicate ¡

Server ¡ Server ¡is ¡listening ¡on ¡ a ¡socket ¡ (socket ¡= ¡IP ¡address ¡ ¡ ¡ ¡+ ¡protocol ¡ ¡ ¡ ¡+ ¡port) ¡ ¡ Port ¡80 ¡= ¡HTTP ¡ Client ¡2 ¡ Client ¡1 ¡

  • Client ¡2 ¡makes ¡

connecLon ¡over ¡ socket ¡

  • Server ¡receives ¡

connecLon, ¡moves ¡ communicaLons ¡ to ¡own ¡socket ¡

  • Server ¡returns ¡to ¡

listening ¡ ¡

  • Server ¡talking ¡to ¡

client ¡1 ¡and ¡2 ¡ ready ¡for ¡others ¡

IP: ¡1.2.3.4 ¡ HTTP ¡ Port: ¡80 ¡

slide-7
SLIDE 7

7 ¡

Java ¡provides ¡a ¡convenient ¡Socket ¡class ¡

WWWSocket.java ¡

  • Run, ¡type ¡~tjp/cs10/index.php ¡
  • Output ¡stream ¡= ¡from ¡your ¡computer ¡to ¡somewhere ¡else ¡
  • ut.println ¡sends ¡data ¡to ¡another ¡computer ¡
  • Input ¡stream ¡= ¡from ¡another ¡computer ¡to ¡your ¡computer ¡
  • in.readLine ¡reads ¡data ¡sent ¡to ¡your ¡computer ¡
slide-8
SLIDE 8

8 ¡

Agenda ¡

  • 1. Sockets ¡
  • 2. Server ¡
  • 3. MulLthreaded ¡server ¡
  • 4. Chat ¡server ¡
slide-9
SLIDE 9

9 ¡

We ¡can ¡create ¡our ¡own ¡server ¡

Server ¡

IP: ¡localhost ¡ TCP ¡ Port: ¡4242 ¡

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

  • ut.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 Socket sock = listen.accept(); System.out

  • ut.println

.println("someone connected" "someone connected"); ); // 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())); ()));

  • ut.println("who is it?");

String line; while while (( ((line line = = in in.readLine .readLine()) != ()) != null null) { ) { System.out

  • ut.println

.println("received:" "received:" + + line line); );

  • ut.println("hi " + line + "! anybody else there?");

} System.out

  • ut.println

.println("client hung up" "client hung up"); ); // Clean up shop

  • ut.close();

in.close(); sock.close(); listen.close(); } ¡

HelloServer.java ¡

Run, ¡then ¡from ¡terminal ¡type ¡telnet ¡localhost ¡4242 ¡

slide-10
SLIDE 10

10 ¡

We ¡can ¡also ¡create ¡our ¡own ¡client ¡too ¡

HelloServer.java ¡and ¡HelloClient.java ¡ HelloServer.java ¡

slide-11
SLIDE 11

11 ¡

We ¡can ¡create ¡our ¡own ¡client ¡too ¡

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

  • ut.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

  • ut.println

.println("...connected" "...connected"); ); // Now listen and respond String line; while while (( ((line line = = in in.readLine .readLine()) != ()) != null null) { ) { // Output what you read System.out

  • ut.println

.println(line line); ); // Get some more input (from the user) to write to the open socket (server) String name = console.nextLine();

  • ut.println(name);

} System.out

  • ut.println

.println("server hung up" "server hung up"); ); // Clean up shop

  • ut.close();

in.close(); sock.close(); } } ¡

HelloClient.java ¡

Run ¡HelloServer.java ¡ Then ¡run ¡HelloClient.java ¡ ¡

Client ¡ ¡

slide-12
SLIDE 12

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 ¡ ¡

slide-13
SLIDE 13

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? ¡
slide-14
SLIDE 14

14 ¡

Agenda ¡

  • 1. Sockets ¡
  • 2. Server ¡
  • 3. MulLthreaded ¡server ¡
  • 4. Chat ¡server ¡
slide-15
SLIDE 15

15 ¡

Currently ¡our ¡server ¡can ¡only ¡handle ¡one ¡ client ¡at ¡a ¡Lme ¡

  • 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 ¡
  • f ¡a ¡program ¡(more ¡on ¡threads ¡next ¡class) ¡
  • Inherit ¡from ¡Thread ¡class ¡and ¡override ¡run ¡method ¡
  • Start ¡thread ¡using ¡start ¡method ¡

Using ¡Java’s ¡Thread ¡mechanism ¡to ¡overcome ¡single ¡client ¡issue ¡

slide-16
SLIDE 16

16 ¡

We ¡can ¡create ¡a ¡“Communicator” ¡on ¡a ¡ separate ¡thread ¡for ¡each ¡connecLon ¡

One ¡Communicator ¡allocated ¡for ¡a ¡single ¡client ¡

slide-17
SLIDE 17

17 ¡

We ¡can ¡create ¡a ¡“Communicator” ¡on ¡a ¡ separate ¡thread ¡for ¡each ¡connecLon ¡

MulGple ¡Communicators ¡allocated ¡for ¡mulGple ¡clients ¡

slide-18
SLIDE 18

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 ¡

slide-19
SLIDE 19

19 ¡

Agenda ¡

  • 1. Sockets ¡
  • 2. Server ¡
  • 3. MulLthreaded ¡server ¡
  • 4. Chat ¡server ¡
slide-20
SLIDE 20

20 ¡

Goal: ¡Chat ¡server ¡allows ¡communicaLon ¡ between ¡mulLple ¡clients ¡

Client ¡sends ¡message ¡ to ¡server ¡

Message ¡

slide-21
SLIDE 21

21 ¡

Goal: ¡Chat ¡server ¡allows ¡communicaLon ¡ between ¡mulLple ¡clients ¡

Server ¡broadcasts ¡ message ¡to ¡all ¡clients ¡

Message ¡ Message ¡

slide-22
SLIDE 22

22 ¡

Client ¡listens ¡for ¡keyboard ¡on ¡main ¡thread ¡ creates ¡Communicator ¡on ¡second ¡thread ¡

Client ¡ Client ¡uses ¡two ¡threads: ¡

  • 1. Listen ¡for ¡keyboard ¡input ¡

(blocks ¡in ¡between ¡entries) ¡

  • 2. Communicates ¡with ¡server ¡
slide-23
SLIDE 23

23 ¡

ChatServer ¡creates ¡a ¡Communicator ¡for ¡ each ¡client ¡

Server ¡

slide-24
SLIDE 24

24 ¡

ChatServer ¡handles ¡mulLple ¡clients ¡and ¡ broadcasts ¡message ¡to ¡each ¡client ¡

Client ¡and ¡server ¡

slide-25
SLIDE 25

25 ¡

ChatServer ¡handles ¡mulLple ¡clients ¡and ¡ broadcasts ¡message ¡to ¡each ¡client ¡

ChatServer.java ¡

  • Starts ¡thread ¡with ¡ChatServerCommunicator on ¡each ¡connecLon ¡
  • Tracks ¡all ¡new ¡threads ¡in ¡comms ¡ArrayList ¡of ¡

ChatServerCommunicators

  • Calls ¡send ¡method ¡on ¡each ¡ChatServerCommunicator ¡when ¡

messages ¡arrive ¡from ¡any ¡client ¡(except ¡self) ¡

  • Provides ¡add ¡and ¡removeCommunicator ¡methods ¡for ¡

ChatServerCommunicator ¡to ¡call ¡

¡

ChatServerCommunicator.java ¡

  • Similar ¡to ¡MultithreadedServerCommunicator
  • Tracks ¡ChatServer ¡that ¡started ¡it ¡
  • Has ¡send ¡method ¡to ¡output ¡messages ¡sent ¡by ¡Server ¡
  • Calls ¡broadcast ¡on ¡ChatServer ¡when ¡new ¡message ¡typed ¡
  • Calls ¡remove ¡on ¡ChatServer ¡when ¡client ¡hangs ¡up ¡
slide-26
SLIDE 26

26 ¡

ChatServer ¡handles ¡mulLple ¡clients ¡and ¡ broadcasts ¡message ¡to ¡each ¡client ¡

ChatClient.java ¡

  • Starts ¡thread ¡with ¡ChatClientCommunicator ¡
  • Listens ¡for ¡keyboard ¡input ¡on ¡main ¡thread
  • Gets ¡name ¡as ¡first ¡input ¡
  • Sends ¡subsequent ¡keyboard ¡input ¡to ¡Server ¡via ¡

ChatClientCommunicator ¡send ¡method ¡

¡

ChatClientCommunicator.java ¡

  • Tracks ¡client ¡that ¡created ¡it ¡
  • Listens ¡for ¡incoming ¡messages ¡and ¡outputs ¡to ¡console ¡in ¡run
  • send ¡method ¡sends ¡console ¡text ¡entered ¡by ¡keyboard ¡to ¡Server ¡

for ¡broadcast ¡

slide-27
SLIDE 27

27 ¡

slide-28
SLIDE 28

28 ¡

We ¡can ¡build ¡a ¡Chat ¡server ¡that ¡will ¡ broadcast ¡messages ¡to ¡all ¡clients ¡

  • Client ¡connects ¡to ¡server ¡and ¡gives ¡name ¡
  • Server ¡now ¡broadcasts ¡messages ¡to ¡all ¡clients, ¡afribuLng ¡message ¡

to ¡client ¡name ¡

  • Server ¡side ¡works ¡similarly ¡to ¡HelloMulLthreadedServer.java, ¡but ¡

keeps ¡track ¡of ¡all ¡threads ¡it ¡creates ¡using ¡comms ¡ArrayList ¡of ¡

Communicators

  • Communicators ¡are ¡removed ¡if ¡client ¡hangs ¡up ¡
  • Each ¡communicator ¡has ¡a ¡send ¡method ¡that ¡the ¡server ¡can ¡call ¡to ¡

send ¡a ¡message ¡to ¡it ¡

  • Adding ¡and ¡removing ¡communicators ¡use ¡synchronized ¡to ¡make ¡

sure ¡only ¡one ¡talks ¡at ¡a ¡Lme ¡(more ¡about ¡this ¡next ¡class) ¡ Chat ¡server ¡

slide-29
SLIDE 29

29 ¡

Clients ¡must ¡listen ¡for ¡both ¡keyboard ¡input ¡ and ¡message ¡from ¡server ¡

  • Clients ¡use ¡two ¡threads ¡
  • Main ¡thread ¡listens ¡for ¡keyboard ¡input ¡
  • Second ¡thread ¡listens ¡for ¡messages ¡from ¡Server ¡
  • Create ¡a ¡“communicator” ¡for ¡the ¡client ¡side ¡ ¡

Chat ¡client ¡