Simple Socket Server#466
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a simple socket communication implementation with a server that can handle a single client connection. The implementation demonstrates basic TCP socket programming concepts including server setup, client connection, and echo functionality.
- Adds a complete socket server and client implementation in Python
- Includes comprehensive documentation with usage instructions
- Updates the main repository README to reference the new implementation
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| Single Client Socket Server/server.py | Implements TCP socket server that accepts one client and echoes received data |
| Single Client Socket Server/client.py | Implements client that connects to server and sends a test message |
| Single Client Socket Server/README.md | Provides documentation and usage instructions for the socket implementation |
| README.md | Adds entry in the main repository table referencing the new socket server |
| 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.
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.
| 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). |
|
|
||
| ```bash | ||
| python server.py | ||
| ```` |
There was a problem hiding this comment.
The markdown code block closing has four backticks instead of three, which may cause rendering issues.
| ```` |
| 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 |
There was a problem hiding this comment.
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.
| 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 |
| 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 |
There was a problem hiding this comment.
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 |
PR Title
Simple One Client Socket Server
Summary
Creates a simple socket server and client
Description
The changes are as follows:
Checks
in the repository
in the PR
Thank You,
Nakul Desai