Python Network Programming on Linux - Simultaneous use of two network interface on the client?

StackOverflow https://stackoverflow.com/questions/14504445

  •  17-01-2022
  •  | 
  •  

Question

Currently I have two network interfaces on my client. I created two separate sockets which binds to each interface ip. Now I also have two servers with each one talking to one socket on the client. I want to be able to receive data (~10MB for now but will be larger later) from both interfaces simultaneously (currently using UDP or DGRAMS) but for some reason there is always a delay in that it has to finish the data transfer with one socket before it begins the transfer on the other socket.

I have confirmed that binding each interface is successful and I am able to communicate on each socket with its respective server. Currently I simply execute s1.recv() and s2.recv() within the same code block.

So my question is that, is the delay due to the "blocking" feature of using recv()? Also through my search it seems there are ways to make the function to be unblocking? But I am not certain how to go about doing that... And would this be resolved by using multithreading? I am a bit rusty with multithread but if it resolves my issue I will look into it. If not then how can I utilize both interfaces in that I am able to receive/send from both interfaces in parallel?

If anyone can assist I would greatly appreciate it.

No correct solution

OTHER TIPS

The recv function is blocking by default. This means that the method will halt the control flow, until something is received on this socket. If you call sock1.recv() and then sock2.recv(), the second call will only be reached when socket 1 receives something.

You can use the setblocking method to make recv non-blocking, causing it to return at once, even if there is nothing to receive. However, then you would probably have to poll both sockets for new bytes to read:

s1.setblocking(False)
s2.setblocking(False)

while True:
    # In non-blocking mode, recv throws a socket.error when
    # there is nothing to receive.
    try: a = s1.recv()
    except socket.error: a = None

    try: b = s2.recv()
    except socket.error: b = None

    handle_data(a,b) # Use received bytes at your discretion

You could parallelize the two recv calls, using either the multiprocessing or threading modules:

class SocketThread(threading.Thread):
    def run():
        while True:
            a = self.sock.recv() # This time, recv in blocking mode
            handle_data(a)

t1, t2 = SocketThread(), SocketThread()
t1.sock = sock1
t1.start()

t2.sock = sock2
t2.start()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top