Verbindungsabbau erst nach 'STOP' string von client

This commit is contained in:
Heimbs 2019-10-15 15:02:44 +02:00
parent e6ac9acf6e
commit e165a09042
2 changed files with 39 additions and 20 deletions

40
protocol-client.py Normal file → Executable file
View File

@ -1,24 +1,36 @@
#!/usr/bin/env python #!/usr/bin/env python3
import socket import socket
HOST = socket.gethostname() HOST = socket.gethostname()
PORT = 60000 PORT = 60001
SOCKET = socket.socket() SOCKET = socket.socket()
msg = input("Your Message: ") try:
SOCKET.connect((HOST, PORT))
msg = ""
SOCKET.connect((HOST, PORT)) while True:
SOCKET.send(msg.encode("utf-8")) msg = input("Your Message: ")
msg = msg.strip()
SOCKET.send(msg.encode("utf-8"))
recv_msg = "" if msg == "STOP":
while True: break
data = SOCKET.recv(16)
if data:
recv_msg += data.decode("utf-8")
else:
break
print(recv_msg)
SOCKET.close() """while True:
data = SOCKET.recv(16)
print(data)
if data:
recv_msg += data.decode("utf-8")
else:
break"""
data = SOCKET.recv(1024)
print(data.decode("utf-8"))
SOCKET.close()
finally:
SOCKET.close()

19
protocol-server.py Normal file → Executable file
View File

@ -1,9 +1,9 @@
#! /usr/bin/env python #! /usr/bin/env python3
import socket import socket
HOST = socket.gethostname() HOST = socket.gethostname()
PORT = 60000 PORT = 60001
SOCKET = socket.socket() SOCKET = socket.socket()
@ -13,10 +13,17 @@ try:
while True: while True:
conn, addr = SOCKET.accept() conn, addr = SOCKET.accept()
msg = conn.recv(1024) print(f"Client {addr[0]} connected at port {addr[1]}.")
upper_msg = msg.decode("utf-8").upper() while True:
print(upper_msg) #print("Running...")
conn.send(upper_msg.encode("utf-8")) msg = conn.recv(1024)
msg = msg.decode("utf-8")
upper_msg = msg.upper()
if msg and msg == "STOP":
break
else:
conn.send(upper_msg.encode("utf-8"))
conn.close() conn.close()
print("Client disconnected. Waiting for new client")
finally: finally:
SOCKET.close() SOCKET.close()