CS ¡10: ¡ Problem ¡solving ¡via ¡Object ¡Oriented ¡ Programming ¡
Winter ¡2017 ¡
¡
Tim ¡Pierson ¡
260 ¡(255) ¡Sudikoff ¡
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.
¡
260 ¡(255) ¡Sudikoff ¡
2 ¡
3 ¡
IP: ¡1.2.3.4 ¡ HTTP ¡ Port: ¡80 ¡
4 ¡
IP: ¡1.2.3.4 ¡ HTTP ¡ Port: ¡80 ¡
5 ¡
IP: ¡1.2.3.4 ¡ HTTP ¡ Port: ¡80 ¡
6 ¡
IP: ¡1.2.3.4 ¡ HTTP ¡ Port: ¡80 ¡
7 ¡
8 ¡
9 ¡
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
.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
.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())); ()));
String line; while while (( ((line line = = in in.readLine .readLine()) != ()) != null null) { ) { System.out
.println("received:" "received:" + + line line); );
} System.out
.println("client hung up" "client hung up"); ); // Clean up shop
in.close(); sock.close(); listen.close(); } ¡
Run, ¡then ¡from ¡terminal ¡type ¡telnet ¡localhost ¡4242 ¡
10 ¡
11 ¡
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
.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
.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
.println(line line); ); // Get some more input (from the user) to write to the open socket (server) String name = console.nextLine();
} System.out
.println("server hung up" "server hung up"); ); // Clean up shop
in.close(); sock.close(); } } ¡
Run ¡HelloServer.java ¡ Then ¡run ¡HelloClient.java ¡ ¡
12 ¡
13 ¡
14 ¡
15 ¡
16 ¡
17 ¡
18 ¡
19 ¡
20 ¡
Message ¡
21 ¡
Message ¡ Message ¡
22 ¡
23 ¡
24 ¡
25 ¡
ChatServerCommunicators
ChatServerCommunicator ¡to ¡call ¡
26 ¡
ChatClientCommunicator ¡send ¡method ¡
27 ¡
28 ¡
Communicators
29 ¡