Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Sockets

Creates UDP and TCP connections over a local network for peer discovery and messaging between machines.

Overview

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.

Features

  • UDP listener — listens on port 8082 for broadcast "hello" messages from other peers on the network.
  • TCP listener — listens on port 8082 for direct incoming connections.
  • Optional broadcasting — periodically announces Hello, I am <ip> and my number is <num> over UDP broadcast every 5 seconds (with -b flag).
  • 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.

Requirements

  • Python 3
  • A running Redis server (reachable at localhost with default settings)
  • Python dependencies listed in requirements.txt

Installation

git clone https://github.com/turtleSimulator3000/Sockets.git
cd Sockets
pip install -r requirements.txt

Make 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.

Usage

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.

Example

python3 app.py 5 -b

Interactive commands

Once 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

How it works

  1. On startup, the app detects its own local IP address using a dummy UDP socket connection.
  2. It connects to a local Redis instance to persist discovered peer IPs.
  3. 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.
  4. If broadcasting is enabled, a third thread (sendBroadcasts()) periodically sends a UDP broadcast announcing this machine's IP and identifier.
  5. The main thread runs an interactive loop for manually connecting to peers or inspecting known connections.

Project structure

Sockets/
├── app.py             # Main application: UDP/TCP listeners, broadcaster, CLI
└── requirements.txt    # Python dependencies (redis)

Notes

  • 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.

About

Creates UDP and TCP connections over local network

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages