Sockets

  • Sockets are a realtime bidirectional communication channel over TCP

  • The Socket class represents the client, and the ServerSocket class represents the server

  • The Socket connects to an IP address and port

  • The ServerSocket listens on a port for incoming connections

    • It doesn’t need to know its own IP address

  • Prep

    • git checkout main (ensure you’re on the main branch)

    • git pull origin main (pull any latest changes)

    • git checkout -b M4-Sockets-Part1 (create a new branch for this module)

Communication Flow

  • Flow Steps:

    • ServerSocket listens on a port

    • ServerSocket calls the blocking accept() method to accept an incoming connection

    • Socket attempts to connect to an open ServerSocket

    • Once accepted, the connection is established

    • The connection closes when the respective side’s close() method is called, or if an exception occurs

    • Threads are important for managing multiple connections, but we’ll cover them later

socket flow

Our First Sample

  • The Client class represents the client (user-side)

  • The Server class represents the server (host-side)

  • The goal: get the server to receive a message from the client

  • Run both through the command line in separate instances

    • It’s best to leverage VS Code’s split terminal for this example and all future ones

Client

  • Note: The provided code is more than most minimal examples you’ll find online

    • The client runs standalone and won’t try to connect immediately

  • Uses the Scanner class to process special commands:

    • /connect ip-address:port connects to a server (use localhost:3000 for local tests)

    • /quit terminates the client

  • After connecting, the PrintWriter class sends data to the server via the socket’s outputStream()

    • PrintWriter wraps the Socket 's output stream (this is the communication channel to the Server)

  • Client code: https://github.com/MattToegel/IT114-2025/blob/M4-Sockets-Part1/M4/Part1/Client.java

Server

  • The server listens on a port for incoming connections

    • We have it default to port 3000

  • It immediately starts accepting connections once it’s running

    • It waits indefinitely for an incoming Socket connection

  • It uses BufferedReader to receive simple Strings

    • Remember: Part 1 is simply demonstrating receiving data

    • For example sake, the Server checks for a /kill server message to terminate itself otherwise it prints the received message

  • Take note of which parts of the code are "blocking"; examples will build up to a full-fledged multi-client server

  • Server code: https://github.com/MattToegel/IT114-2025/blob/M4-Sockets-Part1/M4/Part1/Server.java

Running the Sample

  • Note: Going forward it’s strongly encouraged to use the CLI instead of the IDE’s "run" button

  • Command line steps (assumes content is in a M4 folder with a child Part1 folder):

    • javac M4/Part1/Server.java (compile server)

    • java M4.Part1.Server port(optional) (run server)

    • javac M4/Part1/Client.java (compile client)

    • java M4.Part1.Client (run client)

    • Use /connect localhost:3000 to connect to the server

    • Use /kill server to turn off the server or /quit to terminate the client

  • Remember: Note the difference between javac using the file system path and java using the classpath

Summary

  • Part 1 focuses on isolating specific topics before adding complexity (i.e., connecting and receiving data)

  • In Part 2, we’ll explore how to get a reply from the server (to the client)

  • Play around with the code and ensure you understand each part before proceeding

  • Part 1 Checkpoint: https://github.com/MattToegel/IT114-2025/tree/M4-Sockets-Part1

  • Wrap Up

    • git add M4 (track changes)

    • git commit -m "add M4-Sockets-Part1 sample code" (commit changes)

    • git push origin M4-Sockets-Part1 (push changes to GitHub)

    • Create a pull request to main and merge it

    • git checkout main (switch back to main branch)

    • git pull origin main (pull latest changes)