Socket Programming

Jun 4, 2024

Socket Programming

Overview

  • Sockets: API between application layer code and transport layer services in Internet architecture.
  • Topics Covered:
    • Socket abstraction
    • Programming UDP and TCP sockets
    • Example of a simple client-server application in Python using sockets

Importance of Sockets

  • Only API between application layer and transport layer.
  • Needed to access the Internet's transport layer services to send application layer messages.
  • Application written in user space; transport layer operates within the OS.
  • Interface between the application layer program and the transport layer within the OS.

Transport Layer Services

  • TCP (Transmission Control Protocol)
    • Reliable
    • Congestion-controlled
    • Flow-controlled
    • Byte-stream oriented
    • Data transfer from one process to another
  • UDP (User Datagram Protocol)
    • Datagram-oriented
    • Unreliable service
    • Possible data loss/order misordering
    • No connection (no handshaking)

Example: Simple Client-Server Application

UDP

  • Client: Reads characters from keyboard, sends to server
  • Server: Receives data, converts to uppercase, sends back
  • Client: Receives modified data and displays it
  • Note: No IP directory service (must know IP and port)
    • UDP service: IP address/port must be explicitly included
    • Client socket created without specifying port (OS can assign)

TCP

  • Connection-oriented: Requires handshake before data flow
  • Server opens a "welcoming" socket for initial contact
    • Creates new socket for each client post-handshake
    • New socket uses same port as welcoming socket
  • Reliable in-order byte stream transfer

Programming Example

UDP Client

  1. Include Python socket module
  2. Specify server name and port
  3. Create UDP socket
  4. Get user input and use sendto to send it
  5. Receive reply via recvfrom
  6. Print message and close socket

UDP Server

  1. Include Python socket module
  2. Create UDP socket
  3. Bind socket to specific port
  4. Enter loop to wait for client messages
  5. Translate messages to uppercase and send back
  6. Close connection socket post-exchange

TCP Client

  1. Include Python socket module
  2. Specify server name and port
  3. Create TCP socket
  4. Connect to server using connect
  5. Send input message using send
  6. Receive reply using recv
  7. Print message and close socket

TCP Server

  1. Include Python socket module
  2. Create TCP socket
  3. Bind socket to port
  4. Listening mode
  5. Accept client connections (creates new socket)
  6. Read, process, send reply, and close new socket

Highlights

  • TCP: Uses accept for creating a socket post-client connection
  • UDP: Uses recvfrom and sendto

Application Layer Recap

  1. Basic Structuring: Client-server vs Peer-to-Peer
  2. Application Requirements
    • Delay, reliable data transfer, bandwidth
  3. Transport Services: TCP (reliable) vs UDP (unreliable)
  4. Application Layer Protocols: HTTP, SMTP, DNS, BitTorrent, etc.
  5. Video Streaming
    • Timing considerations, CDNs
  6. Socket Programming Steps
    • Sockets API, abstraction, using UDP and TCP
  7. Protocols Overview
    • Client-server interaction
    • Protocol message format (header + payload)
  8. Key Themes
    • Centralized vs decentralized
    • Stateful vs stateless
    • Scalability, reliable data transfer
    • Pushing complexity to the network edge

Next Topic

  • Moving down to the transport layer