💻

Understanding Sockets in Networking

Jul 31, 2024

Lecture Notes on Sockets

Overview

  • Focus on fundamentals of sockets.
  • Discuss client-server architecture, IP addresses, protocols, and coding in Python.
  • Build a basic client-server system in Python.

Key Concepts

Client and Server

  • Client: Requests services from a server.
  • Server: Provides services to clients.
  • Client-Server Architecture:
    • Structure programs with clients (computers, phones, etc.) connecting to a server.
    • Example: PC1 sends a message to PC2 through the server, not directly.
    • Server can act as a chat room, gaming server, etc.

IP Addresses

  • Local IP Address: An address within a local area network (LAN).
    • Example: 192.168.0.15
  • Public IP Address: Unique address used for internet connectivity.
    • Example: 76.28.44.211 (not a real address).
  • Local IP addresses are not routable from outside the network.

Ports

  • Ports serve as "doors" to access services on an IP address.
  • Example: Connecting to 192.168.0.15:9090.
  • Server must listen on the specified port to accept connections.

Sockets

  • Definition: Communication endpoints for exchanging data.
  • Types of Sockets:
    • AF_INET: Internet sockets using IPv4.
    • AF_INET6: Internet sockets using IPv6.
    • Other types include Bluetooth and infrared sockets.
  • Socket Types:
    • SOCK_STREAM: TCP protocol (connection-based).
    • SOCK_DGRAM: UDP protocol (datagram-based).

TCP vs. UDP

  • TCP (Transmission Control Protocol):

    • Reliable, connection-based.
    • Detects packet loss and maintains order.
    • Suitable for applications requiring full data integrity (e.g., file transfer).
  • UDP (User Datagram Protocol):

    • Unreliable, datagram-based.
    • Allows packets to arrive out of order; no delivery guarantees.
    • Faster, suitable for real-time applications (e.g., video streaming, gaming).

When to Use

  • Use TCP when data loss cannot be tolerated (e.g., file transfers).
  • Use UDP when speed is critical and some data loss is acceptable (e.g., live video calls).

Implementation in Python

Server Implementation

  1. Import socket module.
  2. Create a socket and bind it to local IP and port.
  3. Use listen() to accept incoming connections.
  4. Use a loop to accept connections and handle communication.
  5. Example code snippet for server: import socket server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((host, port)) server.listen(5) while True: comm_socket, addr = server.accept() print(f'Connected to {addr}') message = comm_socket.recv(1024).decode('utf-8') print(f'Message from client: {message}') comm_socket.send('Got your message, thank you'.encode('utf-8')) comm_socket.close()

Client Implementation

  1. Import socket module.
  2. Create a socket and connect to the server's IP and port.
  3. Send a message to the server and receive a response.
  4. Example code snippet for client: import socket client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((host, port)) client.send('Hello, World!'.encode('utf-8')) response = client.recv(1024).decode('utf-8') print(response) client.close()

Summary

  • Understanding sockets is crucial for building networking applications.
  • TCP ensures reliable communication, while UDP focuses on speed and efficiency.
  • Implementation in Python is straightforward and can be adapted for various applications.