Skip to content

Commit c22f4d3

Browse files
author
Francesco Mecatti
committed
Ctrl-C termination
1 parent da2b97d commit c22f4d3

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

ProtocolImplementation/ClientProtocolImplementation/ClientProtocol.py

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def input(self):
1919
raise ConnectionError("Server not confirming")
2020
if not self.status_handler.is_code("End", self.s.recv(CODEBYTES)):
2121
raise ConnectionError("Server not ending")
22+
except KeyboardInterrupt:
23+
raise KeyboardInterrupt
2224
except:
2325
print("Error with transmission. Try again")
2426

client.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@ def start(self):
2121
file_handler.download()
2222

2323
def main():
24-
ip, port = input("Ip address: "), input("Port: ")
24+
try:
25+
ip, port = input("Ip address: "), input("Port: ")
26+
except KeyboardInterrupt:
27+
print("\n\nStopping...")
28+
exit()
2529
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2630
try:
2731
s.connect((ip, int(port)))
2832
print(f"Connected to {s.getsockname()}")
2933
user_handler = UserHandler(s)
3034
user_handler.start()
35+
except KeyboardInterrupt:
36+
print("\n\nExiting...")
3137
finally:
3238
s.close()
3339

server.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def handle(self):
5151
h/H: Help
5252
q: Exit""")
5353
elif cmd == 'q':
54+
protocol_handler.close_connection()
5455
break
5556
else:
5657
protocol_handler.send_output("Command not found")
@@ -67,7 +68,11 @@ def main():
6768
datefmt="%H:%M:%S")
6869
logging.info("Server running...")
6970
server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
70-
server.serve_forever()
71+
try:
72+
server.serve_forever()
73+
except KeyboardInterrupt:
74+
print("\nStopping server. Waiting for the termination of all open connections...")
75+
server.server_close()
7176

7277

7378
if __name__ == "__main__":

0 commit comments

Comments
 (0)