threading.Thread(
target=self.client_loop,
- kwargs={
- "link": link,
- "nb": self.nb_accepts,
- },
+ kwargs={"link": link, "client_nb": self.nb_accepts},
daemon=True,
).start()
self.nb_accepts += 1
- def client_loop(self, link, nb):
- link.send(f"HELLO #{nb}")
+ def client_loop(self, link, client_nb):
+ link.send(f"HELLO #{client_nb}")
try:
while True:
r = link.receive()
- print(f'from #{nb} receive "{r}"')
+ print(f'from #{client_nb} receive "{r}"')
link.send(f"ACK {r}")
if r == "BYE":
break
except EOFError:
- print(f"closing #{nb} on EOF")
+ print(f"closing #{client_nb} on EOF")
######################################################################
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.connect((server_hostname, port))
self.link = SocketConnection(server_socket)
+ self.buffer = []
threading.Thread(target=self.receive, daemon=True).start()
-
- self.send()
- # threading.Thread(target=self.send, daemon=True).start()
+ self.loop()
def receive(self):
try:
while True:
- x = self.link.receive()
- print(f'CultureClient receive "{x}"')
+ self.buffer.append(self.link.receive())
except EOFError:
- print(f"closing connection on EOF")
+ print(f"** closing connection on EOF **")
- def send(self):
+ def loop(self):
try:
- self.link.send(f"HELLO")
- x = 0
while True:
- time.sleep(5)
- print(f'CultureClient send "{x}"')
- self.link.send(x)
- x += 1
+ self.link.send(f"PING {time.localtime().tm_sec}")
+
+ try:
+ while True:
+ print(self.buffer.pop(0))
+ except IndexError:
+ pass
+
+ time.sleep(1)
+
except BrokenPipeError:
- print(f"closing connection on broken pipe")
+ print(f"** closing connection on broken pipe **")
######################################################################