-
-
Notifications
You must be signed in to change notification settings - Fork 344
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
base: main
Are you sure you want to change the base?
Simple Socket Server #466
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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). | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The documentation incorrectly states the port as 12345, but the actual code uses port 65432. This should be updated to reflect the correct port number.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
|
||||||
### 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. | ||||||
|
||||||
|
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}") |
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+10
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The server only handles one client connection and exits after that client disconnects. Consider wrapping the accept() call in a loop to handle multiple sequential connections.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback
Comment on lines
+14
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The server doesn't handle potential socket exceptions that could occur during recv() operations. Consider adding error handling for better user experience.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The markdown code block closing has four backticks instead of three, which may cause rendering issues.
Copilot uses AI. Check for mistakes.