Verbindungsabbau erst nach 'STOP' string von client
This commit is contained in:
parent
e6ac9acf6e
commit
e165a09042
40
protocol-client.py
Normal file → Executable file
40
protocol-client.py
Normal file → Executable file
@ -1,24 +1,36 @@
|
||||
#!/usr/bin/env python
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import socket
|
||||
|
||||
HOST = socket.gethostname()
|
||||
PORT = 60000
|
||||
PORT = 60001
|
||||
|
||||
SOCKET = socket.socket()
|
||||
|
||||
msg = input("Your Message: ")
|
||||
try:
|
||||
SOCKET.connect((HOST, PORT))
|
||||
msg = ""
|
||||
|
||||
SOCKET.connect((HOST, PORT))
|
||||
SOCKET.send(msg.encode("utf-8"))
|
||||
while True:
|
||||
msg = input("Your Message: ")
|
||||
msg = msg.strip()
|
||||
SOCKET.send(msg.encode("utf-8"))
|
||||
|
||||
recv_msg = ""
|
||||
while True:
|
||||
data = SOCKET.recv(16)
|
||||
if data:
|
||||
recv_msg += data.decode("utf-8")
|
||||
else:
|
||||
break
|
||||
print(recv_msg)
|
||||
if msg == "STOP":
|
||||
break
|
||||
|
||||
|
||||
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
19
protocol-server.py
Normal file → Executable file
@ -1,9 +1,9 @@
|
||||
#! /usr/bin/env python
|
||||
#! /usr/bin/env python3
|
||||
|
||||
import socket
|
||||
|
||||
HOST = socket.gethostname()
|
||||
PORT = 60000
|
||||
PORT = 60001
|
||||
|
||||
SOCKET = socket.socket()
|
||||
|
||||
@ -13,10 +13,17 @@ try:
|
||||
|
||||
while True:
|
||||
conn, addr = SOCKET.accept()
|
||||
msg = conn.recv(1024)
|
||||
upper_msg = msg.decode("utf-8").upper()
|
||||
print(upper_msg)
|
||||
conn.send(upper_msg.encode("utf-8"))
|
||||
print(f"Client {addr[0]} connected at port {addr[1]}.")
|
||||
while True:
|
||||
#print("Running...")
|
||||
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()
|
||||
print("Client disconnected. Waiting for new client")
|
||||
finally:
|
||||
SOCKET.close()
|
||||
|
Loading…
x
Reference in New Issue
Block a user