-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
111 lines (89 loc) · 3.13 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
import socket
import threading
chat = []
all_threads = []
channels= {}
groups= {}
connections = {}
def handle_client(conn, addr):
while(True):
print("[thread] starting")
# recv message
try:
message = conn.recv(1024)
except:
break
message = message.decode()
print("[thread] client:", addr, 'recv:', message)
l = []
x = message.split(';')
if(x[0] == "chatinfo"):
connections[x[1]] = conn
chat.append(x[1])
print(x[1], " connected...")
if(x[0] == "createchannel"):# message type : createchannel;channelname
if(x[1] not in channels):
channels[x[1]] = []
channels[x[1]].append(conn)
print(x[1])
if(x[0] == "joinchannel"):# message type : joinchannel;channelname;username
channels[x[1]].append(connections[x[2]])
print(x[2],' joined...')
if(x[0] == "sendtochannel"): # message type sendtochannel;channelname;username;message
for i in channels[x[1]]:
msg = ";".join(x)
print(msg)
i.send(msg.encode())
if(x[0] == "creategroup"):# message type : creategroup;groupname
if(x[1] not in groups):
groups[x[1]] = []
groups[x[1]].append(conn)
print(x[1])
if(x[0] == "joingroup"):# message type : joingroup;groupname;username
groups[x[1]].append(connections[x[2]])
print(x[2],' joined...')
if(x[0] == "sendtogroup"): # message type sendtogroup;groupname;username;message
for i in groups[x[1]]:
msg = ";".join(x)
print(msg)
i.send(msg.encode())
if(x[0] == "getChat"):
l.append("getChat")
for i in chat:
if(i == x[1]):
l.append(i)
for i in channels:
if(i.startswith(x[1])):
l.append(i)
for i in groups:
if(i.startswith(x[1])):
l.append(i)
if(x[0] == "send"):
connections[x[1]].send(message.encode())
continue
message = ";".join(l)
print(message)
message = message.encode()
conn.send(message)
print("[thread] client:", addr, 'send:', message)
host = '127.0.0.1'
port = 12345
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen()
try:
while True:
print("Waiting for client")
conn, addr = s.accept()
print("Client:", addr)
t = threading.Thread(target=handle_client, args=(conn, addr))
t.start()
all_threads.append(t)
except KeyboardInterrupt:
print("Stopped by Ctrl+C")
finally:
if s:
s.close()
for t in all_threads:
t.join()