-
Notifications
You must be signed in to change notification settings - Fork 1
/
relay.py
68 lines (50 loc) · 2.21 KB
/
relay.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
from socket import *
from time import sleep
# Message 1 byte Header
#PING = '00'
#PONG = '01'
PRIV_IP = '02'
USER_INFO = '03'
# keep users' socket and private IP
sockets = {}
priv_IPs = {}
serverSock = socket(AF_INET, SOCK_STREAM)
serverSock.bind(('', 8080)) # use 8080 port
serverSock.listen()
while True:
try:
clientSock, addr = serverSock.accept()
msg = clientSock.recv(1024).decode('utf-8') # receive user's private IP
if(msg[0:2] == PRIV_IP): # if header is fine
print('new connection from ',str(addr))
priv_IP = msg[2:]
delete_list = []
user_info_msg = USER_INFO # header
for _addr in sockets:
try:
# send new user's info to other users
sockets[_addr].send( (USER_INFO + priv_IP + ',' + addr[0] + ',' + str(addr[1]) + '/').encode('utf-8') )
# send current users' info to new user
user_info_msg += priv_IPs[_addr]+','+_addr[0]+','+str(_addr[1])+'/'
except Exception: # if this current user's socket is not available, append this to delete list
sockets[_addr].close()
delete_list.append(sockets[_addr])
for sock in delete_list: # Prevent Runtime Error: dictionary changed size during iteration(above for statement)
del sock
try:
if(user_info_msg != USER_INFO): # if user_info_msg == USER_INFO header, then it means there are no current users
print(user_info_msg) # for testing
clientSock.send(user_info_msg.encode('utf-8'))
# register new member's socket and info to lists
sockets[addr] = clientSock
priv_IPs[addr] = priv_IP
except Exception: # if new member's socket is not available, close this socket
clientSock.close()
except KeyboardInterrupt:
# close every socket
clientSock.close()
for socket in sockets.values():
socket.close()
print("stop server")
serverSock.close()
break