You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
2.5 KiB
61 lines
2.5 KiB
import socket
|
|
import threading
|
|
|
|
HOST = ''
|
|
PORT = 2005
|
|
MAX_CONNECTIONS = 3
|
|
MAX_DINERO = 120000
|
|
connections = 0
|
|
max_connections_reached = False # Add a flag for max connections reached
|
|
|
|
def handle_client(client_socket, addr):
|
|
global connections
|
|
global MAX_DINERO
|
|
global max_connections_reached # Access the global flag
|
|
print(f"cliente conectado {addr})")
|
|
connections += 1
|
|
if connections < MAX_CONNECTIONS:
|
|
max_connections_reached = False # Reset the flag when connections drop below max
|
|
print(f"Conexiones activas: {connections}")
|
|
client_socket.settimeout(60) # Set a timeout of 60 seconds
|
|
while True:
|
|
try:
|
|
datos = client_socket.recv(1024)
|
|
if not datos:
|
|
print("No data received from client. Closing connection.")
|
|
break # Close the connection when no data is received
|
|
decoded_data = datos.decode('utf-8')
|
|
if ' ' in decoded_data:
|
|
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."
|
|
client_socket.sendall(response.encode('utf-8'))
|
|
else:
|
|
print(f"Received unexpected data: {decoded_data}")
|
|
except socket.timeout:
|
|
print("Client did not send data within the timeout period.")
|
|
connections -= 1
|
|
print(f"Conexiones activas: {connections}")
|
|
client_socket.close()
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
s_addr = (HOST, PORT)
|
|
s.bind(s_addr)
|
|
s.listen(MAX_CONNECTIONS)
|
|
while True:
|
|
if connections >= MAX_CONNECTIONS:
|
|
if not max_connections_reached: # Only print the message if the flag is False
|
|
print('Maximo de conexiones alcanzado, esperando a que se libere una conexion.')
|
|
max_connections_reached = True # Set the flag to True after printing the message
|
|
continue
|
|
client_socket, addr = s.accept()
|
|
client_thread = threading.Thread(target=handle_client, args=(client_socket, addr))
|
|
client_thread.daemon = True
|
|
client_thread.start() |