-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver
39 lines (32 loc) · 990 Bytes
/
server
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
# server
import socket
from threading import Thread
ipAddress = 'localhost'
portNumber = 9979
server = socket.socket()
print("socket created.")
server.bind((ipAddress, portNumber))
server.listen(3)
print("Waiting for the connections...")
clientAddresses = []
chatHistory = []
def broardCast(message):
for clientAddress in clientAddresses:
clientAddress.send(str.encode(message + "\n"))
def transferMessages(client):
while True:
message = client.recv(1024).decode()
chatHistory.append(message)
broardCast(message)
clientCounter = 0
while True:
client, clientAddress = server.accept()
clientCounter += 1
name = client.recv(1024).decode()
broardCast( name + " Joined The Chat Room")
print("connected with", name, "of Ip address", clientAddress)
print("Connected With", clientCounter, "Clients till Now.")
for message in chatHistory:
client.send(str.encode(message + "\n"))
clientAddresses.append(client)
Thread(target = transferMessages, args = (client, )).start()