A collection of simple client-server socket programming examples in Python demonstrating both TCP and UDP protocols.
This repository contains basic implementations of network communication using Python's socket library. It includes both TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) examples with client and server components.
sockets_tcp_udp/
├── tcp/
│ ├── tcp_server.py # TCP server implementation
│ └── tcp_client.py # TCP client implementation
└── udp/
├── udp_server.py # UDP server implementation
└── udp_client.py # UDP client implementation
-
TCP Socket Examples: Reliable, connection-oriented communication
- Server accepts one client connection at a time
- Client sends messages to server
- Proper connection handling and cleanup
-
UDP Socket Examples: Fast, connectionless communication
- Server receives messages from any client
- Client sends messages without establishing connection
- Lightweight message passing
- Python 3.x
- No external dependencies (uses built-in
socketlibrary)
python tcp/tcp_server.pyThe server will listen on localhost:5000 and wait for client connections.
In a new terminal:
python tcp/tcp_client.py- Type messages and press Enter to send them to the server
- Use
CTRL+Xto exit the client
python udp/udp_server.pyThe server will listen on localhost:5000 for UDP messages.
In a new terminal:
python udp/udp_client.py- Type messages and press Enter to send them to the server
- Use
CTRL+Xto exit the client
Both implementations use the following default settings:
- Host:
127.0.0.1(localhost) for clients, empty string for servers (all interfaces) - Port:
5000
You can modify the HOST and PORT variables in each file to change these settings.
| Feature | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented | Connectionless |
| Reliability | Guaranteed delivery | Best effort |
| Overhead | Higher | Lower |
| Use Case | When data integrity is critical | When speed is more important |
Conectado por ('127.0.0.1', 54321)
('127.0.0.1', 54321) b'Hello TCP Server!'
Finalizando conexao do cliente ('127.0.0.1', 54321)
('127.0.0.1', 54321) b'Hello UDP Server!'
These examples are designed for learning socket programming concepts:
- Understanding the difference between TCP and UDP
- Basic client-server architecture
- Network programming in Python
- Socket creation, binding, listening, and data transmission
This project is open source and available under the MIT License.