-
Notifications
You must be signed in to change notification settings - Fork 2
/
serverOrigin.py
127 lines (97 loc) · 4.51 KB
/
serverOrigin.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
116
117
118
119
120
121
122
123
124
125
126
127
import socket
import struct
import pickle
import threading
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('192.168.1.4', 50490))
server_socket.listen(4)
clients_connected = {}
clients_data = {}
count = 1
def connection_requests():
global count
while True:
print("Waiting for connection...")
client_socket, address = server_socket.accept()
print(f"Connections from {address} has been established")
print(len(clients_connected))
if len(clients_connected) == 4:
client_socket.send('not_allowed'.encode())
client_socket.close()
continue
else:
client_socket.send('allowed'.encode())
try:
client_name = client_socket.recv(1024).decode('utf-8')
except:
print(f"{address} disconnected")
client_socket.close()
continue
print(f"{address} identified itself as {client_name}")
clients_connected[client_socket] = (client_name, count)
image_size_bytes = client_socket.recv(1024)
image_size_int = struct.unpack('i', image_size_bytes)[0]
client_socket.send('received'.encode())
image_extension = client_socket.recv(1024).decode()
b = b''
while True:
image_bytes = client_socket.recv(1024)
b += image_bytes
if len(b) == image_size_int:
break
clients_data[count] = (client_name, b, image_extension)
clients_data_bytes = pickle.dumps(clients_data)
clients_data_length = struct.pack('i', len(clients_data_bytes))
client_socket.send(clients_data_length)
client_socket.send(clients_data_bytes)
if client_socket.recv(1024).decode() == 'image_received':
client_socket.send(struct.pack('i', count))
for client in clients_connected:
if client != client_socket:
client.send('notification'.encode())
data = pickle.dumps(
{'message': f"{clients_connected[client_socket][0]} joined the chat", 'extension': image_extension,
'image_bytes': b, 'name': clients_connected[client_socket][0], 'n_type': 'joined', 'id': count})
data_length_bytes = struct.pack('i', len(data))
client.send(data_length_bytes)
client.send(data)
count += 1
t = threading.Thread(target=receive_data, args=(client_socket,))
t.start()
def receive_data(client_socket):
while True:
try:
data_bytes = client_socket.recv(1024)
except ConnectionResetError:
print(f"{clients_connected[client_socket][0]} disconnected")
for client in clients_connected:
if client != client_socket:
client.send('notification'.encode())
data = pickle.dumps({'message': f"{clients_connected[client_socket][0]} left the chat",
'id': clients_connected[client_socket][1], 'n_type': 'left'})
data_length_bytes = struct.pack('i', len(data))
client.send(data_length_bytes)
client.send(data)
del clients_data[clients_connected[client_socket][1]]
del clients_connected[client_socket]
client_socket.close()
break
except ConnectionAbortedError:
print(f"{clients_connected[client_socket][0]} disconnected unexpectedly.")
for client in clients_connected:
if client != client_socket:
client.send('notification'.encode())
data = pickle.dumps({'message': f"{clients_connected[client_socket][0]} left the chat",
'id': clients_connected[client_socket][1], 'n_type': 'left'})
data_length_bytes = struct.pack('i', len(data))
client.send(data_length_bytes)
client.send(data)
del clients_data[clients_connected[client_socket][1]]
del clients_connected[client_socket]
client_socket.close()
break
for client in clients_connected:
if client != client_socket:
client.send('message'.encode())
client.send(data_bytes)
connection_requests()