Question

I have this code:

class SrvHandler(asyncore.dispatcher_with_send):
    def handle_read(self):
        data = self.recv(1024)
        if data:
            data = str(data, 'utf-8')
            print("Received: " + data)

Now, I have a VBasic application. This application has 2 thread. This two thread works at the same time but uses same connection (static connection declared). Sometimes, this 2 threads sends data at the same time or at lease few delay between miliseconds. But the problem is, Python3 receive this 2 different data at the same time like one single line of data.

For example:

VBasic Thread 1: socket.write("Hello")
VBasic Thread 2: socket.write("12345")

Python3 Data:
   Expected Result:
      First recv: Hello
      Second recv: 12345
   Actual Result:
      First recv: Hello12345

Why Python3 acts like this? What can i do to prevent this mix? What is the reason of this behavior? Is it by design? How can i prevent this? On python3 or VBasic (Client or Server)

Hope this example and code is informative enough.

Was it helpful?

Solution

This is expected behaviour for a streaming service like TCP.

TCP cannot transfer any message longer than one byte.

If you want to transfer any structure longer than one byte, you need a protocol on top of TCP - maybe put a null or something at the end of your strings and send that as well. At the receiving end, concatenate all received bytes until you find a null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top