-
-
Notifications
You must be signed in to change notification settings - Fork 380
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 2 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). | ||||||
|
||||||
| This will start the server on `localhost` and listen on a predefined port (usually `localhost:12345` unless configured otherwise). | |
| This will start the server on `localhost` and listen on a predefined port (usually `localhost:65432` unless configured otherwise). |
| 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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | |
| while True: # Loop to handle multiple sequential connections | |
| 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 |
Copilot
AI
Jul 21, 2025
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 server doesn't handle potential socket exceptions that could occur during recv() operations. Consider adding error handling for better user experience.
| data = conn.recv(1024) # Receive data from the connection | |
| if not data: | |
| break | |
| conn.sendall(data) # Echo the received data back to the client | |
| try: | |
| data = conn.recv(1024) # Receive data from the connection | |
| if not data: | |
| break | |
| conn.sendall(data) # Echo the received data back to the client | |
| except (socket.error, ConnectionResetError) as e: | |
| print(f"Socket error occurred: {e}") # Log the error | |
| break |
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.