Skip to content

Simple Socket Server #466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
50 changes: 50 additions & 0 deletions Single Client Socket Server/README.md
Original file line number Diff line number Diff line change
@@ -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.


11 changes: 11 additions & 0 deletions Single Client Socket Server/client.py
Original file line number Diff line number Diff line change
@@ -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}")
17 changes: 17 additions & 0 deletions Single Client Socket Server/server.py
Original file line number Diff line number Diff line change
@@ -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