-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.py
103 lines (75 loc) · 2.38 KB
/
chat.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
# import socket library
import socket
# import threading library
import threading
# Choose a port that is free
PORT = 5000
#hello
# An IPv4 address is obtained
# for the server.
SERVER = socket.gethostbyname(socket.gethostname())
# Address is stored as a tuple
ADDRESS = (SERVER, PORT)
# the format in which encoding
# and decoding will occur
FORMAT = "utf-8"
# Lists that will contains
# all the clients connected to
# the server and their names.
clients, names = [], []
# Create a new socket for
# the server
server = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
# bind the address of the
# server to the socket
server.bind(ADDRESS)
# function to start the connection
def startChat():
print("server is working on " + SERVER)
# listening for connections
server.listen()
while True:
# accept connections and returns
# a new connection to the client
# and the address bound to it
conn, addr = server.accept()
conn.send("NAME".encode(FORMAT))
# 1024 represents the max amount
# of data that can be received (bytes)
name = conn.recv(1024).decode(FORMAT)
# append the name and client
# to the respective list
names.append(name)
clients.append(conn)
print(f"Name is :{name}")
# broadcast message
broadcastMessage(f"{name} has joined the chat!".encode(FORMAT))
conn.send('Connection successful!'.encode(FORMAT))
# Start the handling thread
thread = threading.Thread(target = handle,
args = (conn, addr))
thread.start()
# no. of clients connected
# to the server
print(f"active connections {threading.activeCount()-1}")
# method to handle the
# incoming messages
def handle(conn, addr):
print(f"new connection {addr}")
connected = True
while connected:
# receive message
message = conn.recv(1024)
# broadcast message
broadcastMessage(message)
# close the connection
conn.close()
# method for broadcasting
# messages to the each clients
def broadcastMessage(message):
for client in clients:
client.send(message)
# call the method to
# begin the communication
startChat()