-
-
Notifications
You must be signed in to change notification settings - Fork 347
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
Conversation
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.
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.
Choose a reason for hiding this comment
The 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.
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). |
Copilot uses AI. Check for mistakes.
|
||
```bash | ||
python server.py | ||
```` |
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.
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 |
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 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 |
Copilot uses AI. Check for mistakes.
data = conn.recv(1024) # Receive data from the connection | ||
if not data: | ||
break | ||
conn.sendall(data) # Echo the received data back to the client |
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 |
Copilot uses AI. Check for mistakes.
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