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.
- Python 3.8 or higher
- Basic understanding of networking concepts (IP addresses, ports, TCP/UDP)
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
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.pyTerminal 2 - Run the client:
python src/tcp/client.pyThe client sends a message to the server, and the server echoes it back.
The UDP example demonstrates broadcasting messages across a network.
Terminal 1 - Start the receiver:
python src/udp/receiver.pyTerminal 2 - Start the sender:
python src/udp/sender.pyThe sender broadcasts messages every second, which are received by the receiver.
| 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) |
AF_INET- IPv4 addressesAF_INET6- IPv6 addresses
SOCK_STREAM- TCP (reliable, connection-oriented)SOCK_DGRAM- UDP (unreliable, connectionless)
| Feature | TCP | UDP |
|---|---|---|
| Connection | Required | Not required |
| Reliability | Guaranteed delivery | Best effort |
| Order | Preserved | Not guaranteed |
| Use case | Web, file transfer | Streaming, gaming |
- Python socket documentation
- Computer Networks course materials
- Computer Networking: A Top-Down Approach
The built version of the lecture can be accessed from the releases section. Each release belongs to a specific semester.
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.