-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
55 lines (46 loc) · 1.65 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
import socket
import threading
port = 9999 # high number
ip = '127.0.0.1' # localhost
server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # create a socket
server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_sock.bind((ip, port)) # bind the socket to address
server_sock.listen()
print('server start!!')
clients = [] # list of clients
c_addr = [] # list of clients' address
def broadcast(msg): # send data to all clients connected to the server
for c in clients:
c.send(msg)
def event_check(client): # event check, if the client enter 'exit', close the client
# else, broadcast that message
while True:
try:
msg = client.recv(1024)
# broadcast(msg)
if msg.decode('utf-8') == "exit":
index = clients.index(client)
clients.remove(client)
temp = c_addr[index]
del c_addr[index]
client.close()
broadcast("{} disconnected!".format(temp).encode('utf-8'))
break
else:
broadcast(msg)
except:
index = clients.index(client)
clients.remove(client)
temp = c_addr[index]
del c_addr[index]
client.close()
broadcast("{} disconnected!".format(temp).encode('utf-8'))
break
while True:
clientsock, addr = server_sock.accept()
print("connected by", addr)
clients.append(clientsock)
c_addr.append(addr)
clientsock.send('Connected to server'.encode('utf-8'))
thread = threading.Thread(target=event_check, args=(clientsock,))
thread.start()