-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
65 lines (54 loc) · 1.98 KB
/
server.py
File metadata and controls
65 lines (54 loc) · 1.98 KB
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
import socket,threading
def recv_and_broadcast(sock,clients) -> None:
"""
Recebe a mensagem de um cliente e reflete ela
com os demais clientes conectados no servidor
Args:
sock = socket object do cliente
"""
clients = clients
conection_client = sock
try:
while True:
msg = conection_client.recv(2000)
if msg:
print(sock.getsockname(),msg.decode())
# espelhar as mensagens para os outros clientes
for client_sock in clients:
if client_sock != conection_client:
try:
client_sock.send(msg)
except:
CLIENTS.remove(client_sock)
except Exception as e:
print("ERRO: ",e)
CLIENTS = []
def server(host="localhost",port=6969) -> None:
"""
Processo principal, são definidas as características do socket,
ele é iniciado, e fica recebendo as mensagens dos usuários e as
reflete para todos clientes
Args:
host (srt): ip do servidor
port (int): porta que deve ser usada
"""
server_address = (host,port)
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(server_address)
print("Iniciando servidor!")
sock.listen(2)
try:
while True:
client_socket,address = sock.accept()
CLIENTS.append(client_socket)
print(f"Connected: {client_socket},{address}")
threading.Thread(target=recv_and_broadcast,args=[client_socket,CLIENTS]).start()
except Exception as e:
print("Houve um problema : ",e)
finally:
# em caso de algum problema
for client in CLIENTS:
sock.remove(client)
sock.close()
server()