Skip to content

Introduction to socket programming with python - use for one or two hands on session in computer networking course

License

Notifications You must be signed in to change notification settings

1995parham-teaching/socket.py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

socket.py

GitHub release (latest by date)

Introduction

Introduction to socket programming with Python, used for hands-on sessions in the Computer Networking course at Amirkabir University of Technology.

This repository provides practical examples of network communication using Python's socket module. It covers both TCP (connection-oriented) and UDP (connectionless) protocols, helping students understand low-level network programming without spending too much time on setup.

The chosen language is Python, which is also used in the textbook Computer Networking: A Top-Down Approach by Jim Kurose and Keith Ross.

Prerequisites

  • Python 3.8 or higher
  • Basic understanding of networking concepts (IP addresses, ports, TCP/UDP)

Directory Structure

socket.py/
├── README.md
├── LICENSE
├── lecture/
│   └── main.tex          # LaTeX source for lecture notes
└── src/
    ├── tcp/
    │   ├── server.py     # Multi-threaded TCP echo server
    │   └── client.py     # TCP echo client
    └── udp/
        ├── sender.py     # UDP broadcast sender
        └── receiver.py   # UDP broadcast receiver

Quick Start

TCP Echo Server & Client

The TCP example demonstrates a simple echo server that receives messages and sends them back to the client.

Terminal 1 - Start the server:

python src/tcp/server.py

Terminal 2 - Run the client:

python src/tcp/client.py

The client sends a message to the server, and the server echoes it back.

UDP Broadcast

The UDP example demonstrates broadcasting messages across a network.

Terminal 1 - Start the receiver:

python src/udp/receiver.py

Terminal 2 - Start the sender:

python src/udp/sender.py

The sender broadcasts messages every second, which are received by the receiver.

What You'll Learn

Topic Description
Socket Creation Using socket.socket() with different address families and socket types
TCP Server Binding, listening, accepting connections, and handling clients
TCP Client Connecting to servers and exchanging data
Multi-threading Handling multiple clients concurrently using threading
UDP Communication Connectionless data transfer and broadcasting
Socket Options Configuring sockets with setsockopt() (e.g., SO_BROADCAST)

Key Concepts

Address Families

  • AF_INET - IPv4 addresses
  • AF_INET6 - IPv6 addresses

Socket Types

  • SOCK_STREAM - TCP (reliable, connection-oriented)
  • SOCK_DGRAM - UDP (unreliable, connectionless)

TCP vs UDP

Feature TCP UDP
Connection Required Not required
Reliability Guaranteed delivery Best effort
Order Preserved Not guaranteed
Use case Web, file transfer Streaming, gaming

Resources

Releases

The built version of the lecture can be accessed from the releases section. Each release belongs to a specific semester.

License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.

About

Introduction to socket programming with python - use for one or two hands on session in computer networking course

Resources

License

Stars

Watchers

Forks

Packages

No packages published