-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
108 lines (84 loc) · 4.06 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
'''
Copyright (C) 19/06/2024 Hary Patrascu
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Last updated: 19/06/2024
Contact: email: harypatrascu.1@gmail.com
'''
import socket
import threading
class Server:
def __init__(self):
# Setup variables
self.HOST = '127.0.0.1'
self.PORT = 50000
self.BYTES = 4096
self.FORMAT = 'utf-8'
self.MAX_CONNECTIONS = 10
# Setting the socket options to allow for reconnection and creating the server socket
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server_socket.bind((self.HOST, self.PORT))
# Variables to store new clients
self.clients = []
self.usernames = []
def handle_client(self, client):
while True:
try:
message = client.recv(self.BYTES).decode(self.FORMAT)
print(f'{self.usernames[self.clients.index(client)]}:{message}')
self.broadcast(f'{self.usernames[self.clients.index(client)]}:{message}', client)
except socket.error as error:
print(error)
# Remove username and client from the arrays
user_index = self.clients.index(client)
self.clients.remove(client)
username = self.usernames[user_index]
self.usernames.remove(username)
# Update the connections list when someone disconnects
self.broadcast_connections()
print(f'[SERVER] {username} disconnected.')
self.broadcast(f'[SERVER] {username} disconnected.', client)
break
client.close()
def broadcast(self, message, this_client):
for client in self.clients:
if client != this_client:
client.send(message.encode(self.FORMAT))
def broadcast_connections(self):
message = f'NEW_CONNECTION:{','.join(self.usernames)}'
for client in self.clients:
client.send(message.encode(self.FORMAT))
def receive(self):
# Listen for maximum 10 new connections
self.server_socket.listen(self.MAX_CONNECTIONS)
while True:
# Accept a new connection
client, address = self.server_socket.accept()
print(f'[SERVER] New connection from {address}')
# Get the username of the new client
username = client.recv(self.BYTES).decode(self.FORMAT)
# Append the new variables to the storage lists
self.usernames.append(username)
self.clients.append(client)
# Send the username list to the client to be appended to the active connections list
self.broadcast_connections()
# Let everyone know someone joined and show the new client that the connection was successful
print(f'[SERVER] {username} joined the chat.')
self.broadcast(f'{self.usernames[self.clients.index(client)]} joined the chat.', client)
client.send('[SERVER] Connected.'.encode(self.FORMAT))
# Start a server thread
server_thread = threading.Thread(target=self.handle_client, args=(client,))
server_thread.start()
if __name__ == '__main__':
server = Server()
server.receive()
server.server_socket.close()