forked from sharunrajeev/YourFirstContribution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathser_cli2cli.py
54 lines (43 loc) · 1.01 KB
/
ser_cli2cli.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
import socket
import threading
port= 5050
add = ('127.0.0.1', port)
leave_msg = "bye"
active = True
server = socket.socket()
server.bind(add)
client_list = []
def serverside():
server.listen(5)
print(f"Server is listening...")
while active:
conn, addr = server.accept()
client_list.append(conn)
name=conn.recv(1024).decode()
thread = threading.Thread(target=handle_client, args=(conn, name))
thread.start()
print(f"Number of active connections: {threading.active_count() - 1}")
def sendToAllClients(msg,conn):
for client in client_list:
if conn!=client:
client.send(msg.encode())
def handle_client(conn, name):
try:
msg=f"{name} has joined the chat!"
print(msg)
sendToAllClients(msg,conn)
while True:
msg = conn.recv(1024).decode()
if msg == leave_msg:
break
msg = (f"{name}:{msg}")
print(msg)
sendToAllClients(msg,conn)
msg=f"{name} has left the chat!"
print(msg)
sendToAllClients(msg,conn)
client_list.remove(conn)
conn.close()
except:
return
serverside()