Browse Source

Verbindungsabbau erst nach 'STOP' string von client

devel
Heimbs 5 years ago
parent
commit
e165a09042
2 changed files with 39 additions and 20 deletions
  1. 26
    14
      protocol-client.py
  2. 13
    6
      protocol-server.py

+ 26
- 14
protocol-client.py View 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()

+ 13
- 6
protocol-server.py View 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…
Cancel
Save