-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
75 lines (57 loc) · 2.17 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
import sys
import os
from datetime import datetime
from threading import Thread
from socket import socket, AF_INET, SOCK_STREAM
# invalid arguments
if len(sys.argv) < 2:
print("Invalid inputs. Please stick to the format shown below.")
print("$ python3 client.py <portnumber>")
print("portnumber: the port of the chatroom server.")
sys.exit(0)
# define server address
server_name = "127.0.0.1"
server_port = int(sys.argv[1])
# create a new TCP socket and bind it to server address
server_socket = socket(AF_INET, SOCK_STREAM)
server_socket.bind((server_name, server_port))
# start listening on the bound port
server_socket.listen(1)
print(f"TCP server is listening on port {server_port}.")
peers = []
def newUser(conn_socket: socket):
name: str = conn_socket.recv(1024).decode()
# alert peers about new connection
for peer in peers:
if peer != conn_socket:
peer.send(f"{name} has joined the chat\n".encode())
while True:
# receive the message and broadcast it in the network
msg: str = conn_socket.recv(1024).decode()
time = datetime.now().strftime("%H:%M:%S")
# broadcast the data to all peers in the network
for peer in peers:
if peer != conn_socket:
timed_msg = f"[{time}] {name}\n" + msg
peer.send(timed_msg.encode())
# terminate server-side connection with user
if msg == "bye\n":
peers.remove(conn_socket)
conn_socket.close()
return
while True:
try:
# a new client request received on server_port
# once accepted, a random port is assigned for further communication
conn_socket, _ = server_socket.accept()
peers.append(conn_socket)
# create a thread and start its execution
Thread(target=newUser, args=(conn_socket,)).start()
except KeyboardInterrupt:
# alert clients to close their connection
for peer in peers:
peer.send("__SERVER_ERROR__".encode())
server_socket.close()
print("Server has stopped listening.")
# current process is stopped, any spawned threads are also killed
os._exit(0)