This is EchoSocket, a project that facilitates two-way communication between a server and multiple clients. All in the look for faster realtime server-client communication.
-
Simple Two-Way Communication:
- Enables real-time communication between a master server and multiple clients.
- Clients can send and receive messages seamlessly, fostering efficient interactions.
-
Modular Task Execution:
- Register custom task functions on the server for dynamic execution.
- Clients submit tasks, and the server executes corresponding functions, allowing for easy extensibility.
-
Graceful Server Shutdown:
- Ensures a controlled termination process upon server shutdown requests.
- Closes client connections gracefully, maintaining stability in the system.
- Python (version 3.x recommended)
- Git (optional)
-
Clone the repository:
git clone https://github.com/eddiegulay/EchoSocket.git
or download and extract the ZIP file.
-
Navigate to the project directory:
cd EchoSocket
-
Install dependencies (if any):
# we use sockets for this project. it's a built-in module, so no need to install anything!
EchoSocket/
|-- basic/
| |-- client.py
| |-- server.py
| |-- multi_clients.py
| |-- single_action.py
|-- master.py
|-- slave.py
|-- src/
| |-- client.py
| |-- server.py
|-- |-- jobs.py
|-- assets/
| |-- echoSocket.jpg
|-- LICENSE
|-- .gitignore
|-- README.md
|-- *_server.py*
|-- *_client.py*
- basic: Contains basic client and server scripts.
- src: Contains server and client classes plus server jobs (The actual project)
- _server.py: Where the server script is located.
- _client.py: Where the client script is located.
-
Create Server Instance:
In your
_server.py
file, create an instance of the server and define your custom task functions.# _server.py from src.server import Server from src.jobs import reverse_string_task if __name__ == "__main__": # Create a server instance master = Server("127.0.0.1", 8080, "Master") # Register task functions master.register_task_function('reverse', reverse_string_task) # you can register as many functions as you can # Run the server master.run()
-
Run the Master Server:
Execute the
_server.py
script to start the master server.python _server.py
-
Start a Slave Client:
In your
_client.py
file, create an instance of the client and send a task.# _client.py from src.client import Client if __name__ == "__main__": # Create a client instance client = Client("127.0.0.1", 8080, "The Client") # Send a task to reverse a string client.send_task('reverse', 'Hello, EchoSocket!') # Wait for the response from the server response = client.client_socket.recv(1024).decode('utf-8') print(f"Reversed Output: {response}") # Close the client connection client.close_client()
-
Run the Client:
Execute the
_client.py
script to start the slave client.python _client.py