Skip to content

add server and client #2

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 4 commits into
base: develop
Choose a base branch
from
Open

Conversation

PavelLinearB
Copy link
Contributor

@PavelLinearB PavelLinearB commented Feb 19, 2025

✨ PR Description

Purpose: Implements a basic client-server chat system using socket programming and threading in Python.

Main changes:

  • Created client.py with socket connection and message handling functionality
  • Created server.py with multi-client support using threading and connection management
  • Implemented bidirectional communication with message broadcasting between connected clients

Generated by LinearB AI and added by gitStream.
AI-generated content may contain inaccuracies. Please verify before using. We’d love your feedback! 🚀

@linear-b linear-b deleted a comment from gitstream-cm bot Feb 20, 2025
Copy link

gitstream-cm bot commented Mar 17, 2025

✨ PR Review

Bug - src/client/client.py

Details:

Problem: Missing string literals in print statements and input calls
Fix: Add proper string literals to all strings
Why: Current code will cause syntax errors as strings are not properly quoted

        try:
            data = socket.recv(32)
-            print(str(data.decode(utf-8)))
+            print(str(data.decode("utf-8")))
        except:
-            print(You have been disconnected from the server)
+            print("You have been disconnected from the server")
            signal = False
            break

#Get host and port
-host = input(Host: )
-port = int(input(Port: ))
+host = input("Host: ")
+port = int(input("Port: "))

Security - src/client/client.py

Details:

Problem: Unsafe broad exception handling and no socket cleanup
Fix: Add specific exception handling and proper socket cleanup
Why: Current code could mask important errors and leak resources

+import socket
try:
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))
-except:
-    print(Could not make a connection to the server)
+except socket.error as e:
+    print(f"Could not make a connection to the server: {e}")
    input("Press enter to quit")
    sys.exit(0)
+finally:
+    sock.close()

Performance - src/server/server.py

Details:

Problem: Small fixed buffer size (32 bytes) for receiving data
Fix: Increase buffer size to more reasonable value
Why: Small buffer size could truncate messages and cause poor performance

    def run(self):
        while self.signal:
            try:
-                data = self.socket.recv(32)
+                data = self.socket.recv(4096)
            except:
                print("Client " + str(self.address) + " has disconnected")

Maintainability - src/server/server.py

Details:

Problem: Global variables used without thread synchronization
Fix: Add threading Lock for shared resources
Why: Concurrent access to shared resources could cause race conditions

import socket
import threading

#Variables for holding information about connections
+connections_lock = threading.Lock()
connections = []
total_connections = 0

def newConnections(socket):
    while True:
        sock, address = socket.accept()
        global total_connections
+        with connections_lock:
            connections.append(Client(sock, address, total_connections, "Name", True))
            connections[len(connections) - 1].start()
            total_connections += 1

Generated by LinearB AI and added by gitStream.
AI-generated content may contain inaccuracies. Please verify before using. We’d love your feedback! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant