Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • I need help with TCP sockets (Python)


    una_maquina

    Recommended Posts

    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:

    KuNjmBH.png

    But this is what tends to happen:

    3WwXOmJ.png

    Sometimes no data arrives at all!

    Link to comment
    Share on other sites

    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.

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.