diff --git a/README.md b/README.md index 6f9cbe5..2f3d41f 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,7 @@ More information on contributing and the general code of conduct for discussion | Selfie with Python | [Selfie with Python](https://github.com/DhanushNehru/Python-Scripts/tree/main/Selfie%20with%20Python) | Take your selfie with python . | | Simple DDOS | [Simple DDOS](https://github.com/DhanushNehru/Python-Scripts/tree/main/Simple%20DDOS) | The code allows you to send multiple HTTP requests concurrently for a specified duration. | | Simple TCP Chat Server | [Simple TCP Chat Server](https://github.com/DhanushNehru/Python-Scripts/tree/main/TCP%20Chat%20Server) | Creates a local server on your LAN for receiving and sending messages! | +| Single Client Socket Server | [Single Client Socket Server](https://github.com/DhanushNehru/Python-Scripts/tree/main/Single%20Client%20Socket%20Server) | Creates a local socket server for a single client | | Smart Attendance System | [Smart Attendance System](https://github.com/DhanushNehru/Python-Scripts/tree/main/Smart%20Attendance%20System) | This OpenCV framework is for Smart Attendance by actively decoding a student's QR Code. | | Snake Game | [Snake Game](https://github.com/DhanushNehru/Python-Scripts/tree/main/Snake%20Game) | Classic snake game using python. | | Snake Water Gun | [Snake Water Gun](https://github.com/DhanushNehru/Python-Scripts/tree/main/Snake%20Water%20Gun) | A game similar to Rock Paper Scissors. | diff --git a/Single Client Socket Server/README.md b/Single Client Socket Server/README.md new file mode 100644 index 0000000..f76677a --- /dev/null +++ b/Single Client Socket Server/README.md @@ -0,0 +1,50 @@ + +# Socket Communication - Single Client and Server + +This project demonstrates basic socket programming in Python with a server and a single client. The server waits for a client to connect and then allows two-way communication between the server and the client. + +## 📁 Files + +- `server.py` – Runs the socket server and waits for a client to connect. +- `client.py` – Connects to the server and allows interaction with it. + +## 🛠 Requirements + +- Python 3.x +- No external libraries required (uses built-in `socket` module) + +## 🚀 How to Run + +> ⚠️ Make sure both scripts are in the same directory and run them in separate terminals. + +### 1. Start the Server + +Open a terminal and run: + +```bash +python server.py +```` + +This will start the server on `localhost` and listen on a predefined port (usually `localhost:12345` unless configured otherwise). + +### 2. Start the Client + +In a **different terminal**, run: + +```bash +python client.py +``` + +This will connect the client to the server. + +Once connected, you can exchange messages between the server and the client. + + + +## ❗ Notes + +* Only **one client** is supported at a time. +* If you close either the server or client, the socket connection will be terminated. +* If the server crashes or is stopped, the client will also lose connection. + + diff --git a/Single Client Socket Server/client.py b/Single Client Socket Server/client.py new file mode 100644 index 0000000..fb8e9af --- /dev/null +++ b/Single Client Socket Server/client.py @@ -0,0 +1,11 @@ +import socket + +HOST = "127.0.0.1" # The server's hostname or IP address +PORT = 65432 # The port used by the server + +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.connect((HOST, PORT)) + s.sendall(b"Hello, world") + data = s.recv(1024) + +print(f"Received {data!r}") \ No newline at end of file diff --git a/Single Client Socket Server/server.py b/Single Client Socket Server/server.py new file mode 100644 index 0000000..88705c1 --- /dev/null +++ b/Single Client Socket Server/server.py @@ -0,0 +1,17 @@ +import socket + +HOST = "127.0.0.1" # Standard loopback interface address (localhost) +PORT = 65432 # Port to listen on (non-privileged ports are > 1023) + +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: #AF_INET for IPv4, SOCK_STREAM for TCP + s.bind((HOST, PORT)) # Bind the socket to the address and port + s.listen() # Enable the server to accept connections + print(f"Server listening on {HOST}:{PORT}") + conn, addr = s.accept() # Wait for a connection + with conn: # conn is a new socket object usable to send and receive data + print(f"Connected by {addr}") # Accept the connection + while True: # Loop to handle incoming data + data = conn.recv(1024) # Receive data from the connection + if not data: + break + conn.sendall(data) # Echo the received data back to the client \ No newline at end of file