-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
105 lines (91 loc) · 3.71 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import socket as so
import threading
import zlib
import logging
import time
HOST = "190.169.1.135"
PORT = 5000
TIMEOUT = 10
RETRY_DELAY = 2 # Segundos de espera entre intentos de reconexión
MAX_RETRIES = 3 # Máximo de intentos de reconexión
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
def compress_message(message):
# Comprime el mensaje usando zlib
return zlib.compress(message.encode())
def decompress_message(message):
# Descomprime el mensaje usando zlib
return zlib.decompress(message).decode()
def messages_from_server(client):
# Recibe y muestra mensajes del servidor
try:
while True:
message = client.recv(1024).decode("utf-8")
if message.strip(): # Verifica que el mensaje no esté vacío
username, content = message.split("~", 1)
logging.info(f"[{username}] {content}")
else:
logging.warning("Received an empty message from the server.")
except (ConnectionResetError, ConnectionAbortedError):
logging.error("Connection lost. Exiting.")
except Exception as e:
logging.error(f"An error occurred while receiving message: {e}")
finally:
client.close()
def send_message_to_server(client):
# Envía mensajes de entrada del usuario al servidor
try:
message = input("Message: ").strip()
if message:
client.sendall(message.encode())
else:
print("Empty message. Type something to send.")
except (ConnectionResetError, ConnectionAbortedError):
logging.error("Connection lost. Unable to send message.")
except Exception as e:
logging.error(f"An error occurred while sending message: {e}")
finally:
client.close()
def communicate_to_server(client):
try:
username = input("Enter your username: ").strip()
if username:
client.sendall(username.encode())
else:
print("Username cannot be empty. Exiting.")
client.close()
return
threading.Thread(target=messages_from_server, args=(client,), daemon=True).start()
send_message_to_server(client)
except (ConnectionResetError, ConnectionAbortedError):
logging.error("Connection error occurred. Exiting.")
except Exception as e:
logging.error(f"An error occurred: {e}")
finally:
client.close()
def connect_with_retries():
retries = 0
while retries < MAX_RETRIES:
try:
with so.socket(so.AF_INET, so.SOCK_STREAM) as client:
client.settimeout(TIMEOUT)
logging.info(f"Connecting to {HOST}:{PORT} (Attempt {retries + 1})")
client.connect((HOST, PORT))
communicate_to_server(client)
break # Exit loop if connection is successful
except (so.timeout, so.error) as e:
retries += 1
logging.warning(f"Connection attempt {retries} failed: {e}")
if retries < MAX_RETRIES:
logging.info(f"Retrying in {RETRY_DELAY} seconds...")
time.sleep(RETRY_DELAY)
else:
logging.error("Max retries reached. Could not connect to the server.")
if __name__ == "__main__":
try:
with so.socket(so.AF_INET, so.SOCK_STREAM) as client:
client.connect((HOST, PORT))
client.settimeout(TIMEOUT)
communicate_to_server(client)
except so.error as e:
print(f"Could not connect to the server: {e}")
connect_with_retries()