-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
115 lines (90 loc) · 3.61 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
112
113
114
115
import socket
from _thread import *
import sys
from collections import defaultdict as df
import time
class Server:
def __init__(self):
self.rooms = df(list)
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
def accept_connections(self, ip_address, port):
self.ip_address = ip_address
self.port = port
self.server.bind((self.ip_address, int(self.port)))
self.server.listen(100)
while True:
connection, address = self.server.accept()
print(str(address[0]) + ":" + str(address[1]) + " Connected")
start_new_thread(self.clientThread, (connection,))
self.server.close()
def clientThread(self, connection):
user_id = connection.recv(1024).decode().replace("User ", "")
room_id = connection.recv(1024).decode().replace("Join ", "")
if room_id not in self.rooms:
connection.send("New Group created".encode())
else:
connection.send("Welcome to ChatsApp(R)".encode())
self.rooms[room_id].append(connection)
while True:
try:
message = connection.recv(1024)
print(str(message.decode()))
if message:
if str(message.decode()) == "FILE":
self.broadcastFile(connection, room_id, user_id)
else:
message_to_send = "<" + str(user_id) + "> " + message.decode()
self.broadcast(message_to_send, connection, room_id)
else:
self.remove(connection, room_id)
except Exception as e:
print(repr(e))
print("Client disconnected earlier")
break
def broadcastFile(self, connection, room_id, user_id):
file_name = connection.recv(1024).decode()
lenOfFile = connection.recv(1024).decode()
for client in self.rooms[room_id]:
if client != connection:
try:
client.send("FILE".encode())
time.sleep(0.1)
client.send(file_name.encode())
time.sleep(0.1)
client.send(lenOfFile.encode())
time.sleep(0.1)
client.send(user_id.encode())
except:
client.close()
self.remove(client, room_id)
total = 0
print(file_name, lenOfFile)
while str(total) != lenOfFile:
data = connection.recv(1024)
total = total + len(data)
for client in self.rooms[room_id]:
if client != connection:
try:
client.send(data)
# time.sleep(0.1)
except:
client.close()
self.remove(client, room_id)
print("Sent")
def broadcast(self, message_to_send, connection, room_id):
for client in self.rooms[room_id]:
if client != connection:
try:
client.send(message_to_send.encode())
except:
client.close()
self.remove(client, room_id)
def remove(self, connection, room_id):
if connection in self.rooms[room_id]:
self.rooms[room_id].remove(connection)
if __name__ == "__main__":
ip_address = "192.168.43.118"
port = 12345
s = Server()
s.accept_connections(ip_address, port)