Slide 1

Slide 1 text

jgs CSC 309 Software Engineering II Lecture 16: More Tools II Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.com Building 14 -227 Office Hours: By appointment

Slide 2

Slide 2 text

jgs Previously

Slide 3

Slide 3 text

jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 3 import java.net.*; public class Server implements Runnable{ public void run() { final int PORT = 12345; try { ServerSocket serverSocket = new ServerSocket(PORT)); System.out.println("Server started. Waiting for a client..."); Socket clientSocket = serverSocket.accept(); System.out.println("Client connected: " + clientSocket); BufferedReader in = new BufferedReader( new InputStreamReader(clientSocket.getInputStream())); PrintWriter out = new PrintWriter( clientSocket.getOutputStream(), true); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println("Client: " + inputLine); out.println("Server received: " + inputLine); } } catch (IOException e) { e.printStackTrace(); } } } Server Runnable

Slide 4

Slide 4 text

jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 4 public class Client implements Runnable{ public void run() { final String SERVER_ADDRESS = "localhost"; final int PORT = 12345; try { Socket socket = new Socket(SERVER_ADDRESS, PORT); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream())); BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Connected to server."); String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("Server: " + in.readLine()); } } catch (UnknownHostException e) { System.err.println("Unknown host: " + SERVER_ADDRESS); System.exit(1); } catch (IOException e) { System.err.println("Error connecting to server"); e.printStackTrace(); System.exit(1); } } } Runnable Client

Slide 5

Slide 5 text

jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 5 public static void main(String[] args) { Client server = new Server(port); Thread t1= new Thread(server); t1.start (); Client client = new Client(address, port); Thread t2= new Thread(client); t2.start (); } Multi-Threading

Slide 6

Slide 6 text

jgs Connecting the Dots Pong

Slide 7

Slide 7 text

jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 7 Pong

Slide 8

Slide 8 text

jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 8 Pong 0 :: Timer, Socket, Listeners, UI

Slide 9

Slide 9 text

jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 9 Let’s Work

Slide 10

Slide 10 text

jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 10 Pong 1 :: Timer, Socket, Listeners, UI

Slide 11

Slide 11 text

jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 11 Questions

Slide 12

Slide 12 text

jgs

Slide 13

Slide 13 text

jgs CSC 309 Software Engineering II Lab 14: Work in your Sprint Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.com Building 14 -227 Office Hours: By appointment

Slide 14

Slide 14 text

jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 14 Let’s Work

Slide 15

Slide 15 text

jgs CSC 309 Software Engineering II Javier Gonzalez-Sanchez, Ph.D. [email protected] Winter 2023 Copyright. These slides can only be used as study material for the class CSC308 at Cal Poly. They cannot be distributed or used for another purpose.