Creates UDP and TCP connections over a local network for peer discovery and messaging between machines.
Sockets is a small Python utility that lets a machine advertise itself on the local network (via UDP broadcast) and accept direct connections from peers (via TCP), while tracking discovered peers in Redis. It automatically detects your local IP address and supports an interactive console for connecting to specific peers.
- UDP listener — listens on port
8082for broadcast "hello" messages from other peers on the network. - TCP listener — listens on port
8082for direct incoming connections. - Optional broadcasting — periodically announces
Hello, I am <ip> and my number is <num>over UDP broadcast every 5 seconds (with-bflag). - Peer tracking — newly discovered peer IPs are stored in a Redis list (
connections). - Interactive CLI — connect to a specific IP, list known connections, or exit.
- Automatic IP detection — determines the machine's local network IP without requiring manual configuration.
- Python 3
- A running Redis server (reachable at
localhostwith default settings) - Python dependencies listed in
requirements.txt
git clone https://github.com/turtleSimulator3000/Sockets.git
cd Sockets
pip install -r requirements.txtMake sure a Redis server is running locally before starting the app (e.g. redis-server). If Redis isn't available, the app will print a warning and continue without connection tracking.
python3 app.py NUM [-b]NUM— a numeric identifier greater than 1, used to identify this instance in broadcast messages.-b— optional flag to enable periodic UDP broadcasting of your presence.
python3 app.py 5 -bOnce running, the app prompts for input in the terminal:
| Input | Action |
|---|---|
<IPv4 address> |
Opens a TCP connection to the given peer |
conns |
Lists all known peer connections stored in Redis |
exit |
Stops all listeners/threads and exits |
- On startup, the app detects its own local IP address using a dummy UDP socket connection.
- It connects to a local Redis instance to persist discovered peer IPs.
- Two background threads run continuously:
listenUdp()— receives UDP broadcast messages from other peers and initiates a TCP connection back to new peers.listenTcp()— accepts incoming TCP connections and spawns a receiver thread per connection.
- If broadcasting is enabled, a third thread (
sendBroadcasts()) periodically sends a UDP broadcast announcing this machine's IP and identifier. - The main thread runs an interactive loop for manually connecting to peers or inspecting known connections.
Sockets/
├── app.py # Main application: UDP/TCP listeners, broadcaster, CLI
└── requirements.txt # Python dependencies (redis)
- Both UDP and TCP communication use port
8082. - The app requires network access to send/receive broadcast packets, so it must be run on the same local subnet as intended peers. Sockets_README.md Displaying Sockets_README.md.