parent
c0fb239118
commit
ff3566fec7
@ -1,22 +1,43 @@
|
|||||||
import socket
|
import socket
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
|
||||||
HOST = '192.168.50.166'
|
def get_ip_address():
|
||||||
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
try:
|
||||||
|
s.connect(('10.255.255.255', 1))
|
||||||
|
IP = s.getsockname()[0]
|
||||||
|
except Exception:
|
||||||
|
IP = '127.0.0.1'
|
||||||
|
finally:
|
||||||
|
s.close()
|
||||||
|
return IP
|
||||||
|
|
||||||
|
HOST = get_ip_address()
|
||||||
PORT = 2005
|
PORT = 2005
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||||
server_address = (HOST, PORT)
|
server_address = (HOST, PORT)
|
||||||
s.connect(server_address)
|
try:
|
||||||
command = input("Entra comando (retirar o 'exit' para salir): ")
|
s.connect(server_address)
|
||||||
if command.lower() == 'exit':
|
except ConnectionRefusedError:
|
||||||
break
|
print("Connection refused, try again later.")
|
||||||
elif command.lower() == 'retirar':
|
sys.exit() # Terminate the program
|
||||||
amount = input("Entera la cantidad de dinero: ")
|
# Send a message immediately after connecting
|
||||||
message = f"{command} {amount}"
|
message = "Conectando!"
|
||||||
else:
|
print('{!r}'.format(message))
|
||||||
print("Comando no reconocido")
|
|
||||||
print('Mandando {!r}'.format(message))
|
|
||||||
s.sendall(message.encode('utf-8'))
|
s.sendall(message.encode('utf-8'))
|
||||||
print('Esperando la respuesta')
|
print('Esperando la respuesta')
|
||||||
data = s.recv(1024) # línea bloqueante
|
while True: # Keep the connection open until the user enters a command
|
||||||
print('recibido {!r} de {}'.format(data.decode('utf-8'), server_address))
|
command = input("Entra comando (retirar o 'exit' para salir): ")
|
||||||
|
if command.lower() == 'exit':
|
||||||
|
break
|
||||||
|
elif command.lower() == 'retirar':
|
||||||
|
amount = input("Entera la cantidad de dinero: ")
|
||||||
|
message = f"{command} {amount}"
|
||||||
|
else:
|
||||||
|
print("Comando no reconocido")
|
||||||
|
message = command # Send the unrecognized command to the server
|
||||||
|
s.sendall(message.encode('utf-8'))
|
||||||
|
|
||||||
|
@ -1,22 +1,43 @@
|
|||||||
import socket
|
import socket
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
|
||||||
HOST = '192.168.50.166'
|
def get_ip_address():
|
||||||
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
try:
|
||||||
|
s.connect(('10.255.255.255', 1))
|
||||||
|
IP = s.getsockname()[0]
|
||||||
|
except Exception:
|
||||||
|
IP = '127.0.0.1'
|
||||||
|
finally:
|
||||||
|
s.close()
|
||||||
|
return IP
|
||||||
|
|
||||||
|
HOST = get_ip_address()
|
||||||
PORT = 2005
|
PORT = 2005
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||||
server_address = (HOST, PORT)
|
server_address = (HOST, PORT)
|
||||||
s.connect(server_address)
|
try:
|
||||||
command = input("Entra comando (retirar o 'exit' para salir): ")
|
s.connect(server_address)
|
||||||
if command.lower() == 'exit':
|
except ConnectionRefusedError:
|
||||||
break
|
print("Connection refused, try again later.")
|
||||||
elif command.lower() == 'retirar':
|
sys.exit() # Terminate the program
|
||||||
amount = input("Entera la cantidad de dinero: ")
|
# Send a message immediately after connecting
|
||||||
message = f"{command} {amount}"
|
message = "Conectando!"
|
||||||
else:
|
print('{!r}'.format(message))
|
||||||
print("Comando no reconocido")
|
|
||||||
print('Mandando {!r}'.format(message))
|
|
||||||
s.sendall(message.encode('utf-8'))
|
s.sendall(message.encode('utf-8'))
|
||||||
print('Esperando la respuesta')
|
print('Esperando la respuesta')
|
||||||
data = s.recv(1024) # línea bloqueante
|
while True: # Keep the connection open until the user enters a command
|
||||||
print('recibido {!r} de {}'.format(data.decode('utf-8'), server_address))
|
command = input("Entra comando (retirar o 'exit' para salir): ")
|
||||||
|
if command.lower() == 'exit':
|
||||||
|
break
|
||||||
|
elif command.lower() == 'retirar':
|
||||||
|
amount = input("Entera la cantidad de dinero: ")
|
||||||
|
message = f"{command} {amount}"
|
||||||
|
else:
|
||||||
|
print("Comando no reconocido")
|
||||||
|
message = command # Send the unrecognized command to the server
|
||||||
|
s.sendall(message.encode('utf-8'))
|
||||||
|
|
||||||
|
@ -1,22 +1,43 @@
|
|||||||
import socket
|
import socket
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
|
||||||
HOST = '192.168.50.166'
|
def get_ip_address():
|
||||||
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
try:
|
||||||
|
s.connect(('10.255.255.255', 1))
|
||||||
|
IP = s.getsockname()[0]
|
||||||
|
except Exception:
|
||||||
|
IP = '127.0.0.1'
|
||||||
|
finally:
|
||||||
|
s.close()
|
||||||
|
return IP
|
||||||
|
|
||||||
|
HOST = get_ip_address()
|
||||||
PORT = 2005
|
PORT = 2005
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||||
server_address = (HOST, PORT)
|
server_address = (HOST, PORT)
|
||||||
s.connect(server_address)
|
try:
|
||||||
command = input("Entra comando (retirar o 'exit' para salir): ")
|
s.connect(server_address)
|
||||||
if command.lower() == 'exit':
|
except ConnectionRefusedError:
|
||||||
break
|
print("Connection refused, try again later.")
|
||||||
elif command.lower() == 'retirar':
|
sys.exit() # Terminate the program
|
||||||
amount = input("Entera la cantidad de dinero: ")
|
# Send a message immediately after connecting
|
||||||
message = f"{command} {amount}"
|
message = "Conectando!"
|
||||||
else:
|
print('{!r}'.format(message))
|
||||||
print("Comando no reconocido")
|
|
||||||
print('Mandando {!r}'.format(message))
|
|
||||||
s.sendall(message.encode('utf-8'))
|
s.sendall(message.encode('utf-8'))
|
||||||
print('Esperando la respuesta')
|
print('Esperando la respuesta')
|
||||||
data = s.recv(1024) # línea bloqueante
|
while True: # Keep the connection open until the user enters a command
|
||||||
print('recibido {!r} de {}'.format(data.decode('utf-8'), server_address))
|
command = input("Entra comando (retirar o 'exit' para salir): ")
|
||||||
|
if command.lower() == 'exit':
|
||||||
|
break
|
||||||
|
elif command.lower() == 'retirar':
|
||||||
|
amount = input("Entera la cantidad de dinero: ")
|
||||||
|
message = f"{command} {amount}"
|
||||||
|
else:
|
||||||
|
print("Comando no reconocido")
|
||||||
|
message = command # Send the unrecognized command to the server
|
||||||
|
s.sendall(message.encode('utf-8'))
|
||||||
|
|
||||||
|
@ -1,22 +1,43 @@
|
|||||||
import socket
|
import socket
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
|
||||||
HOST = '192.168.50.166'
|
def get_ip_address():
|
||||||
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
try:
|
||||||
|
s.connect(('10.255.255.255', 1))
|
||||||
|
IP = s.getsockname()[0]
|
||||||
|
except Exception:
|
||||||
|
IP = '127.0.0.1'
|
||||||
|
finally:
|
||||||
|
s.close()
|
||||||
|
return IP
|
||||||
|
|
||||||
|
HOST = get_ip_address()
|
||||||
PORT = 2005
|
PORT = 2005
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||||
server_address = (HOST, PORT)
|
server_address = (HOST, PORT)
|
||||||
s.connect(server_address)
|
try:
|
||||||
command = input("Entra comando (retirar o 'exit' para salir): ")
|
s.connect(server_address)
|
||||||
if command.lower() == 'exit':
|
except ConnectionRefusedError:
|
||||||
break
|
print("Connection refused, try again later.")
|
||||||
elif command.lower() == 'retirar':
|
sys.exit() # Terminate the program
|
||||||
amount = input("Entera la cantidad de dinero: ")
|
# Send a message immediately after connecting
|
||||||
message = f"{command} {amount}"
|
message = "Conectando!"
|
||||||
else:
|
print('{!r}'.format(message))
|
||||||
print("Comando no reconocido")
|
|
||||||
print('Mandando {!r}'.format(message))
|
|
||||||
s.sendall(message.encode('utf-8'))
|
s.sendall(message.encode('utf-8'))
|
||||||
print('Esperando la respuesta')
|
print('Esperando la respuesta')
|
||||||
data = s.recv(1024) # línea bloqueante
|
while True: # Keep the connection open until the user enters a command
|
||||||
print('recibido {!r} de {}'.format(data.decode('utf-8'), server_address))
|
command = input("Entra comando (retirar o 'exit' para salir): ")
|
||||||
|
if command.lower() == 'exit':
|
||||||
|
break
|
||||||
|
elif command.lower() == 'retirar':
|
||||||
|
amount = input("Entera la cantidad de dinero: ")
|
||||||
|
message = f"{command} {amount}"
|
||||||
|
else:
|
||||||
|
print("Comando no reconocido")
|
||||||
|
message = command # Send the unrecognized command to the server
|
||||||
|
s.sendall(message.encode('utf-8'))
|
||||||
|
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
import socket
|
|
||||||
|
|
||||||
HOST = '192.168.50.166'
|
|
||||||
PORT = 2005
|
|
||||||
|
|
||||||
while True:
|
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
||||||
server_address = (HOST, PORT)
|
|
||||||
s.connect(server_address)
|
|
||||||
command = input("Entra comando (retirar o 'exit' para salir): ")
|
|
||||||
if command.lower() == 'exit':
|
|
||||||
break
|
|
||||||
elif command.lower() == 'retirar':
|
|
||||||
amount = input("Entera la cantidad de dinero: ")
|
|
||||||
message = f"{command} {amount}"
|
|
||||||
else:
|
|
||||||
print("Comando no reconocido")
|
|
||||||
print('Mandando {!r}'.format(message))
|
|
||||||
s.sendall(message.encode('utf-8'))
|
|
||||||
print('Esperando la respuesta')
|
|
||||||
data = s.recv(1024) # línea bloqueante
|
|
||||||
print('recibido {!r} de {}'.format(data.decode('utf-8'), server_address))
|
|
@ -1,22 +0,0 @@
|
|||||||
import socket
|
|
||||||
|
|
||||||
HOST = '192.168.50.166'
|
|
||||||
PORT = 2005
|
|
||||||
|
|
||||||
while True:
|
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
||||||
server_address = (HOST, PORT)
|
|
||||||
s.connect(server_address)
|
|
||||||
command = input("Entra comando (retirar o 'exit' para salir): ")
|
|
||||||
if command.lower() == 'exit':
|
|
||||||
break
|
|
||||||
elif command.lower() == 'retirar':
|
|
||||||
amount = input("Entera la cantidad de dinero: ")
|
|
||||||
message = f"{command} {amount}"
|
|
||||||
else:
|
|
||||||
print("Comando no reconocido")
|
|
||||||
print('Mandando {!r}'.format(message))
|
|
||||||
s.sendall(message.encode('utf-8'))
|
|
||||||
print('Esperando la respuesta')
|
|
||||||
data = s.recv(1024) # línea bloqueante
|
|
||||||
print('recibido {!r} de {}'.format(data.decode('utf-8'), server_address))
|
|
@ -1,36 +1,61 @@
|
|||||||
import socket
|
import socket
|
||||||
|
import threading
|
||||||
|
|
||||||
HOST = ''
|
HOST = ''
|
||||||
PORT = 2005
|
PORT = 2005
|
||||||
MAX_CONNECTIONS = 2
|
MAX_CONNECTIONS = 3
|
||||||
MAX_DINERO = 120000
|
MAX_DINERO = 120000
|
||||||
connections = 0
|
connections = 0
|
||||||
#pip install six
|
max_connections_reached = False # Add a flag for max connections reached
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
||||||
s_addr = (HOST, PORT)
|
def handle_client(client_socket, addr):
|
||||||
s.bind(s_addr)
|
global connections
|
||||||
s.listen(MAX_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:
|
while True:
|
||||||
if connections >= MAX_CONNECTIONS:
|
try:
|
||||||
print('Maximo de conexiones alcanzado, esperando a que se libere una conexion.')
|
datos = client_socket.recv(1024)
|
||||||
continue
|
if not datos:
|
||||||
client_socket, addr = s.accept()
|
print("No data received from client. Closing connection.")
|
||||||
connections += 1 # Increment connections after the check
|
break # Close the connection when no data is received
|
||||||
with client_socket:
|
decoded_data = datos.decode('utf-8')
|
||||||
while True:
|
if ' ' in decoded_data:
|
||||||
datos = client_socket.recv(1024)
|
command, amount = decoded_data.split()
|
||||||
if not datos:
|
|
||||||
connections -= 1
|
|
||||||
break
|
|
||||||
print('Cliente connectetado y datos enviados : {} bytes de {}'.format(len(datos), addr))
|
|
||||||
command, amount = datos.decode('utf-8').split()
|
|
||||||
amount = int(amount)
|
amount = int(amount)
|
||||||
if command.lower() == 'retirar':
|
if command.lower() == 'retirar':
|
||||||
print('retiro exitoso')
|
|
||||||
if amount <= MAX_DINERO:
|
if amount <= MAX_DINERO:
|
||||||
MAX_DINERO -= amount
|
MAX_DINERO -= amount
|
||||||
response = f"Retiro Existoso."
|
response = f"Retiro Existoso."
|
||||||
|
print("Retiro Existoso.")
|
||||||
|
print(f"Saldo restante: {MAX_DINERO}")
|
||||||
else:
|
else:
|
||||||
response = "Saldo insuficiente por favor intente mas tarde."
|
response = "Saldo insuficiente por favor intente mas tarde."
|
||||||
client_socket.sendall(response.encode('utf-8'))
|
client_socket.sendall(response.encode('utf-8'))
|
||||||
print('enviados {} bytes de vuelta a {}'.format(len(response), addr))
|
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()
|
Loading…
Reference in new issue