Compare commits

..

No commits in common. "9d8eb0757012ed1bc6c85dee927894b9be12cabc" and "e6d7441c170a8a293dbbbc0113bbf4ce4fdd7634" have entirely different histories.

2 changed files with 15 additions and 24 deletions

View File

@ -60,9 +60,9 @@ if __name__ == "__main__":
read_msg(s)
except ConnectionRefusedError:
print("Could not connect to the server!")
finally:
s.close()
except KeyboardInterrupt:
print("Exit with CTRL+C")
finally:
pass

View File

@ -33,33 +33,24 @@ def handle_connection(conn):
connections.remove(conn)
break
def handle_new_connections():
if __name__ == "__main__":
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.bind((HOST, PORT))
print(f"Server {hostname} on ip {IPAddr}")
while run:
conn=Connection()
s.listen()
ready_to_read, _, _ = select.select([s], [], [], 1.0)
if ready_to_read:
conn.sock, conn.addr = s.accept()
connections.append(conn)
threading.Thread(target=handle_connection, args=(conn,)).start()
except KeyboardInterrupt:
run = False
print("Shutdown server")
finally:
s.close()
if __name__ == "__main__":
threading.Thread(target=handle_new_connections).start()
try:
print("Press Enter or CTRL + C to stop server")
input()
except KeyboardInterrupt:
pass
finally:
run = False
print("Shutdown server")