Añadiendo elemento lock al servidor y cambio pequeño para que no suben el numuero de conexiones cuando alcanza al maximo

master
vicsash 7 months ago
parent 6821bfcde1
commit d3d821cbae

@ -6,18 +6,19 @@ PORT = 2002
MAX_CONNECTIONS = 3 MAX_CONNECTIONS = 3
MAX_DINERO = 120000 MAX_DINERO = 120000
connections = 0 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): def handle_client(client_socket, addr):
global connections global connections
global MAX_DINERO global MAX_DINERO
print(f"cliente conectado {addr})") if connections >= MAX_CONNECTIONS:
connections += 1
if connections > MAX_CONNECTIONS:
response = "Maximo numero de conexiones alcanzado. Por favor intente mas tarde." response = "Maximo numero de conexiones alcanzado. Por favor intente mas tarde."
client_socket.sendall(response.encode('utf-8')) client_socket.sendall(response.encode('utf-8'))
client_socket.close() client_socket.close()
return return
print(f"cliente conectado {addr})")
connections += 1
print(f"Conexiones activas: {connections}") print(f"Conexiones activas: {connections}")
try: try:
while True: while True:
@ -37,6 +38,7 @@ def handle_client(client_socket, addr):
command, amount = decoded_data.split() command, amount = decoded_data.split()
amount = int(amount) amount = int(amount)
if command.lower() == 'retirar': if command.lower() == 'retirar':
with dinero_lock:
if amount <= MAX_DINERO: if amount <= MAX_DINERO:
MAX_DINERO -= amount MAX_DINERO -= amount
response = f"Retiro Existoso." response = f"Retiro Existoso."
@ -67,4 +69,3 @@ while True:
client_socket, addr = s.accept() client_socket, addr = s.accept()
client_thread = threading.Thread(target=handle_client, args=(client_socket, addr)) client_thread = threading.Thread(target=handle_client, args=(client_socket, addr))
client_thread.start() client_thread.start()

Loading…
Cancel
Save

Powered by INFORMATICA.FP.EDU.ES.