From d3d821cbae44260556b49d1f2670ffe017b600a1 Mon Sep 17 00:00:00 2001 From: vicsash Date: Fri, 16 Feb 2024 17:07:30 +0100 Subject: [PATCH] =?UTF-8?q?A=C3=B1adiendo=20elemento=20lock=20al=20servido?= =?UTF-8?q?r=20y=20cambio=20peque=C3=B1o=20para=20que=20no=20suben=20el=20?= =?UTF-8?q?numuero=20de=20conexiones=20cuando=20alcanza=20al=20maximo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Servidor/ServidorCajero.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Servidor/ServidorCajero.py b/Servidor/ServidorCajero.py index e6f44dd..c8bbd1b 100644 --- a/Servidor/ServidorCajero.py +++ b/Servidor/ServidorCajero.py @@ -6,18 +6,19 @@ PORT = 2002 MAX_CONNECTIONS = 3 MAX_DINERO = 120000 connections = 0 -max_connections_reached = False # Add a flag for max connections reached +max_connections_reached = False +dinero_lock = threading.Lock() def handle_client(client_socket, addr): global connections global MAX_DINERO - print(f"cliente conectado {addr})") - connections += 1 - if connections > MAX_CONNECTIONS: + if connections >= MAX_CONNECTIONS: response = "Maximo numero de conexiones alcanzado. Por favor intente mas tarde." client_socket.sendall(response.encode('utf-8')) client_socket.close() return + print(f"cliente conectado {addr})") + connections += 1 print(f"Conexiones activas: {connections}") try: while True: @@ -37,14 +38,15 @@ def handle_client(client_socket, addr): command, amount = decoded_data.split() amount = int(amount) if command.lower() == 'retirar': - if amount <= MAX_DINERO: - MAX_DINERO -= amount - response = f"Retiro Existoso." - print("Retiro Existoso.") - print(f"Saldo restante: {MAX_DINERO}") - else: - response = "Saldo insuficiente por favor intente mas tarde." - print("Saldo insuficiente para operacion.") + with dinero_lock: + if amount <= MAX_DINERO: + MAX_DINERO -= amount + response = f"Retiro Existoso." + print("Retiro Existoso.") + print(f"Saldo restante: {MAX_DINERO}") + else: + response = "Saldo insuficiente por favor intente mas tarde." + print("Saldo insuficiente para operacion.") client_socket.sendall(response.encode('utf-8')) elif command.lower() == 'exit': break @@ -66,5 +68,4 @@ while True: if connections < MAX_CONNECTIONS: client_socket, addr = s.accept() client_thread = threading.Thread(target=handle_client, args=(client_socket, addr)) - client_thread.start() - + client_thread.start() \ No newline at end of file