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) read_msg(s)
except ConnectionRefusedError: except ConnectionRefusedError:
print("Could not connect to the server!") print("Could not connect to the server!")
finally:
s.close()
except KeyboardInterrupt: except KeyboardInterrupt:
print("Exit with CTRL+C") print("Exit with CTRL+C")
finally:
pass

View File

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