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.
33 lines
871 B
33 lines
871 B
import socket
|
|
import threading
|
|
|
|
# Configuración del cliente
|
|
HOST = 'localhost'
|
|
PORT = 5555
|
|
|
|
# Crear un socket del cliente
|
|
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
client_socket.connect((HOST, PORT))
|
|
|
|
def receive_messages():
|
|
"""Recibe mensajes del servidor y muestra en pantalla."""
|
|
while True:
|
|
try:
|
|
message = client_socket.recv(1024).decode('utf-8')
|
|
print(message)
|
|
except:
|
|
# En caso de error, salir del bucle
|
|
break
|
|
|
|
def send_messages():
|
|
"""Envía mensajes al servidor."""
|
|
while True:
|
|
message = input()
|
|
client_socket.send(message.encode('utf-8'))
|
|
|
|
# Iniciar hilos para recibir y enviar mensajes
|
|
receive_thread = threading.Thread(target=receive_messages)
|
|
receive_thread.start()
|
|
|
|
send_thread = threading.Thread(target=send_messages)
|
|
send_thread.start() |