una_maquina 35 Posted July 4, 2021 Basically I'm having buggy TCP sockets, and if I add a time.sleep(0.10), it magically fixes itself, BUT that's stupid. Problem: it's not synchronised, sometimes the data arrives, sometimes it doesn't and just gets lost... The code is in Python: class Main(): END_MESSAGE = "EOXEOEXOXAADSADW" def __init__(self, logging_class_instance): self.logging_class = logging_class_instance def send_data_to_client(self,socket,data,encryption_key): try: self.logging_class.log("sending data to client",self.logging_class.COLOR_DEFAULT) encrypted = AES_encrypt(data+self.END_MESSAGE,encryption_key) self.logging_class.log("sending encrypted data: "+str(encrypted)[0:400],self.logging_class.COLOR_DEFAULT) socket.sendall(encrypted) self.logging_class.log("done sending",self.logging_class.COLOR_GREEN) return True except Exception as e: self.logging_class.log("encountered an error while sending data: "+str(e),self.logging_class.COLOR_RED) return False def receive_data_from_client(self,socket,encryption_key): data = b'' while True: chunk = socket.recv(10240) if not chunk: raise ValueError("Couldn't receive data, socket disconnected") self.logging_class.log("received encrypted data: "+str(chunk),self.logging_class.COLOR_DEFAULT) decrypted_chunk = AES_decrypt(chunk,encryption_key) data += decrypted_chunk if self.END_MESSAGE in decrypted_chunk.decode(): data = data[:-len(self.END_MESSAGE)] break length = len(data) self.logging_class.log("decrypted data: "+str(data)[0:400],self.logging_class.COLOR_DEFAULT) return data Maybe you could recommend me some higher-level networking frameworks or something... (I tried Twisted already, but same thing happens). I also tried using select library, but the same thing happens. How could I know when the client is listening for data -- AND only then send it to the client? And vice-versa? How to make sure data is 10000% transferred and received? It should be like this: But this is what tends to happen: Sometimes no data arrives at all!
una_maquina 35 Author Posted July 6, 2021 Ok nvm, I've fixed the issue myself. What I did: I added sock.recv(1024) after sock.sendall(encrypted_data) in my send data function/method. And I added sock.send(encrypt("OK")) in my receive data function after it receives all the data. Now it's synchronized and all's good.
Recommended Posts
Archived
This topic is now archived and is closed to further replies.