Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSC364 Lecture 03

CSC364 Lecture 03

Introduction to Networked, Distributed, and Parallel Computing
Client-Server
(202601)

Avatar for Javier Gonzalez-Sanchez

Javier Gonzalez-Sanchez PRO

January 09, 2026
Tweet

Transcript

  1. Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.info o ffi ce: 14 -227

    CSC 364 Introduction to Networked, Distributed, and Parallel Computing Lecture 03. Client-Server Architecture 1
  2. 2 Concepts Device A (IP address) 🏭 🏭 🏭 🏭

    Port Port Port Port Port Port Port Port Socket Socket Device B (IP address)
  3. 6 Failure Is Normal Try this: • Kill the server

    • Disconnect the client • Send inv a lid d a t a Common errors: • Connection refused • Timeout
  4. 8 Java Streams • A stre a m is a

    f low of d a t a (from a source → to a destin a tion) • Sequenti a l • One direction • InputStre a m → re a d d a t a • OutputStre a m → write d a t a For sockets: • You get two stre a ms • One for sending • One for receiving The socket itself does not send d a t a , Stre a ms do
  5. 9 Why Streams Exist ➡ We use stre a ms

    to tre a t a ll these uniformly “Write bytes here, they go somewhere.” Then progr a ms c a n inter a ct (s a me w a y) with: • Files • Network connections • Memory bu ff ers • Keybo a rds a nd screens
  6. 10 Common Stream Types in Networking • D a t

    a InputStre a m / D a t a OutputStre a m - Structured bin a ry d a t a • Bu ff eredRe a der / PrintWriter- Hum a n-re a d a ble text • ObjectInputStre a m / ObjectOutputStre a m - J a v a objects
  7. 11 Byte Streams Byte Stre a ms • InputStre a

    m • OutputStre a m • Work with r a w bytes ⚠ Networks send/receive bytes
  8. 12 Byte Streams Using only OutputStre a m: out.write(72); out.write(101);

    out.write(108); out.write(108); out.write(111); This sends "Hello"… but: • H a rd to re a d • Error-prone • No structure ➡ We need higher-level stre a ms
  9. 13 Stream Wrapping (Very Important) J a v a stre

    a ms a re l a yered OutputStream os = socket.getOutputStream(); DataOutputStream dout = new DataOutputStream(os); E a ch l a yer: • Adds fe a tures • Still writes to the s a me destin a tion
  10. 14 What Is DataOutputStream? D a t a OutputStre a

    m a llows you to: • Write primitive types • In a port a ble, structured form a t Ex a mples: • int • double • boole a n • String
  11. 15 DataOutputStream in Your Code From your ex a mple:

    DataOutputStream dout = new DataOutputStream(socket.getOutputStream()); This me a ns: • D a t a goes into dout • dout writes bytes • Bytes go through the socket • Server receives them
  12. 16 Writing Data with DataOutputStream dout.writeUTF("Hello Server”); Wh a t

    h a ppens: 1. String is converted to bytes 2. Length is encoded 3. Bytes a re sent over the network ➡ This a voids a mbiguity on the receiver side
  13. 17 Flushing the Stream dout.flush(); Why f lush? • D

    a t a m a y be bu ff ered • Flush forces d a t a to be sent immedi a tely
  14. 18 Closing Streams dout.close(); socket.close(); Closing a stre a m:

    • Flushes rem a ining d a t a • Rele a ses system resources • Sign a ls end-of-communic a tion ⚠ In network progr a ms, closing often me a ns: “I’m done t a lking.”
  15. 21 Lab Server Running. Waiting for a connection… Client connected.

    Sending: ping Received: pong Client Running. Connecting to server… Connected. Received: ping Sending: pong Socket Socket
  16. CSC 364 Introduction to Introduction to Networked, Distributed, and Parallel

    Computing Javier Gonzalez-Sanchez, Ph.D. [email protected] Winter 2026 Copyright. These slides can only be used as study material for the class CSC 364 at Cal Poly. They cannot be distributed or used for another purpose. 23