-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
171 lines (153 loc) · 4.92 KB
/
server.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import time
import threading
import socket as sock
FINISH = "BEM"
def input_number(message, max_number):
while True:
try:
number = int(input(message))
if number > 0 and number < max_number:
return number
print("Valor fora do intervalo 1-{}".format(max_number))
except ValueError:
print("Entrada inválida!")
def get_buffer(s, packet_name):
data = s.recv(1000).decode()
return int(data[len(packet_name)+1:])
def print_udp(interval, received, expected):
time_passed = interval[1] - interval[0]
received_kb = len(received) * 8 / 1000.0
packet_loss = 100 - received.count("x") * 100.0 / expected
if time_passed:
print("Thread UDP: recebido {} kb no tempo de {} ms com velocidade de {} kb/s. Perda de pacotes: {} %".format(
received_kb,
time_passed * 1000,
received_kb / time_passed,
packet_loss
))
else:
print("Tempo medido muito curto.")
def print_tcp(interval, received):
time_passed = interval[1] - interval[0]
received_kb = len(received) * 8 / 1000.0
if time_passed:
print("Thread TCP: recebido {} kb no tempo de {} ms com velocidade de {} kb/s".format(
received_kb,
time_passed * 1000,
received_kb / time_passed
))
else:
print("Tempo medido muito curto.")
def accept_clients(tcp_socket, udp_socket):
tcp_t = None
udp_t = None
conn = None
while True:
try:
conn = tcp_socket.accept()[0]
if (tcp_t and tcp_t.is_alive()) or (udp_t and udp_t.is_alive()):
conn.send("OCUPADO".encode())
conn.close()
else:
conn.send("OK".encode())
print("Cliente Conectado.")
tcp_t = threading.Thread(target=receive_tcp, args=(conn, ))
udp_t = threading.Thread(target=receive_udp, args=(udp_socket, ))
tcp_t.start()
udp_t.start()
except sock.error as e:
print(e.strerror)
break
if tcp_t and tcp_t.is_alive():
if conn:
conn.shutdown(sock.SHUT_RDWR)
conn.close()
tcp_t.join()
if udp_t and udp_t.is_alive():
udp_t.join()
def receive_udp(udp_socket):
try:
max_buffer_size = get_buffer(udp_socket, 'TAMANHO')
expected = get_buffer(udp_socket, 'TOTAL')
except ValueError as e:
print("[udp]Mensagem incorreta do cliente!")
print(e)
return
except sock.error as e:
print("[udp]" + str(e))
return
received = ""
start = time.time()
while True:
stop = time.time()
try:
data, client = udp_socket.recvfrom(max_buffer_size)
udp_socket.sendto(b"pong", client)
if data.decode() == FINISH:
break
else:
received += data.decode()
except sock.error as e:
print(e)
break
print_udp((start, stop), received, expected)
def receive_tcp(conn):
try:
max_buffer_size = get_buffer(conn, 'TAMANHO')
except ValueError as e:
print("[tcp]Mensagem incorreta do cliente!")
print(e)
conn.close()
return
except sock.error as e:
print("[tcp]" + str(e))
conn.close()
return
received = ""
start = time.time()
while True:
stop = time.time()
try:
data = conn.recv(max_buffer_size)
conn.send(b"pong")
if not data:
print("[tcp]Conexão Terminou Prematuramente!")
break
if data.decode() == FINISH:
break
else:
received += data.decode()
except sock.error:
break
print_tcp((start, stop), received)
conn.close()
def configure_tcp(port):
tcp_socket = sock.socket(sock.AF_INET, sock.SOCK_STREAM)
tcp_socket.setsockopt(sock.SOL_SOCKET, sock.SO_REUSEADDR, 1)
tcp_socket.bind(('', port))
tcp_socket.listen(10)
print('Servidor TCP pronto...')
return tcp_socket
def configure_udp(port):
udp_socket = sock.socket(sock.AF_INET, sock.SOCK_DGRAM)
udp_socket.bind(('', port))
# timeout é necessário para não ficar travado na udp_thread
udp_socket.settimeout(10)
print('Servidor UDP pronto...')
return udp_socket
def main():
port = input_number("Insira a porta: ", 65535)
tcp_socket = configure_tcp(port)
udp_socket = configure_udp(port)
accepting_thread = threading.Thread(target=accept_clients, args=(tcp_socket, udp_socket))
accepting_thread.start()
while True:
c = input("Insira q para sair:\n")
if c == "q":
tcp_socket.shutdown(sock.SHUT_RDWR)
tcp_socket.close()
udp_socket.close()
break
accepting_thread.join()
if __name__ == "__main__":
main()