-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
127 lines (100 loc) · 3.38 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
116
117
118
119
120
121
122
123
124
125
126
127
import socket
import threading
import time
import sys
import os
HEADER = 64
# SERVER = socket.gethostbyname(socket.gethostname())
SERVER = sys.argv[1]
print(SERVER)
PORT = int(sys.argv[2])
FORMAT = 'utf-8'
ADDRESS = (SERVER, PORT)
DISCONNECT_MESSAGE = "!DISCONNECT"
SERVER_TERMINATION_MESSAGE = "SERVER HOST TERMINATED"
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDRESS)
ClientList = []
ClientListLock = threading.Lock()
def send_message(connection,msg):
msg = msg.encode(FORMAT)
message_length = f"{len(msg):<{HEADER}}".encode(FORMAT)
connection.send(message_length + msg)
def recv_message(connection):
message_header = connection.recv(HEADER)
message_length = int(message_header.decode(FORMAT).strip())
msg = connection.recv(message_length).decode(FORMAT)
return msg
def recv_send_file(connection):
file_size = int(recv_message(connection))
print("recv_send_file : file_size : ",file_size)
for c in ClientList:
if c[0]==connection:
continue
send_message(c[0],str(file_size))
temp_size=0
while temp_size < file_size :
#data = recv_message(connection)
data = connection.recv(1024)
temp_size += len(data)
for c in ClientList:
if c[0]==connection:
continue
c[0].send(data)
print("recv_send sucessfull")
def GroupChatClient(connection,address):
ClientNameMsg = recv_message(connection)
print(ClientNameMsg)
with ClientListLock :
ClientList.append((connection,ClientNameMsg))
with ClientListLock:
for c in ClientList:
if c[0]==connection :
continue
send_message(c[0],f"{ClientNameMsg} joined the chat!")
send_message(connection,str(len(ClientList)))
for clients in ClientList :
send_message(connection,clients[1])
try:
connected = True
while connected :
msg = recv_message(connection)
if not msg:
break
if msg == DISCONNECT_MESSAGE:
with ClientListLock:
for c in ClientList:
if c[0]==connection :
continue
send_message(c[0],f"{ClientNameMsg} exited the chat")
connected = False
print(f"[{ClientNameMsg}] {msg}")
with ClientListLock:
for c in ClientList:
if c[0]==connection :
continue
send_message(c[0],f"[{ClientNameMsg}] {msg}")
if msg[0:6] == '!send ':
recv_send_file(connection)
finally :
with ClientListLock :
ClientList.remove((connection,ClientNameMsg))
send_message(connection,msg)
connection.close()
def StartServer():
ClientList = []
print('[SERVER LISTENING]')
server.listen()
try :
while True:
connection , address = server.accept()
thread = threading.Thread(target=GroupChatClient
,args=(connection,address))
thread.start()
except KeyboardInterrupt:
server.close()
print('[SERVER TERMINATED]')
sys.exit()
print('[ACTIVATING SERVER]')
print("For Server Termination, use the Keyboard Interrupt (Ctrl-C)")
StartServer()