|
|
|
@ -1,8 +1,9 @@
|
|
|
|
|
import socket
|
|
|
|
|
import sys
|
|
|
|
|
import threading
|
|
|
|
|
|
|
|
|
|
HOST = ''
|
|
|
|
|
PORT = 2005
|
|
|
|
|
PORT = 2002
|
|
|
|
|
MAX_CONNECTIONS = 3
|
|
|
|
|
MAX_DINERO = 120000
|
|
|
|
|
connections = 0
|
|
|
|
@ -11,21 +12,29 @@ 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
|
|
|
|
|
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"Conexiones activas: {connections}")
|
|
|
|
|
client_socket.settimeout(60) # Set a timeout of 60 seconds
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
datos = client_socket.recv(1024)
|
|
|
|
|
try:
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
datos = client_socket.recv(1024)
|
|
|
|
|
except ConnectionResetError:
|
|
|
|
|
print("Connection was closed by the client.")
|
|
|
|
|
break
|
|
|
|
|
if not datos:
|
|
|
|
|
print("No data received from client. Closing connection.")
|
|
|
|
|
break # Close the connection when no data is received
|
|
|
|
|
break
|
|
|
|
|
decoded_data = datos.decode('utf-8')
|
|
|
|
|
if ' ' in decoded_data:
|
|
|
|
|
if decoded_data.lower() == 'conectando!':
|
|
|
|
|
response = "Connection successful!"
|
|
|
|
|
client_socket.sendall(response.encode('utf-8'))
|
|
|
|
|
elif ' ' in decoded_data:
|
|
|
|
|
command, amount = decoded_data.split()
|
|
|
|
|
amount = int(amount)
|
|
|
|
|
if command.lower() == 'retirar':
|
|
|
|
@ -37,25 +46,25 @@ def handle_client(client_socket, addr):
|
|
|
|
|
else:
|
|
|
|
|
response = "Saldo insuficiente por favor intente mas tarde."
|
|
|
|
|
client_socket.sendall(response.encode('utf-8'))
|
|
|
|
|
elif command.lower() == 'exit':
|
|
|
|
|
break
|
|
|
|
|
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()
|
|
|
|
|
except socket.timeout:
|
|
|
|
|
print("Client did not send data within the timeout period.")
|
|
|
|
|
finally:
|
|
|
|
|
connections -= 1
|
|
|
|
|
print(f"Conexiones activas: {connections}")
|
|
|
|
|
client_socket.close()
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
|
|
|
server_address = (HOST, PORT)
|
|
|
|
|
s.bind(server_address)
|
|
|
|
|
s.listen(MAX_CONNECTIONS)
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
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()
|