We are trying to use Ethernet communication in order to have multiple Arduinos (acting as servers) push alerts to a Raspberry Pi acting as a client) using socket programming. We have the following code but it only prints data1 after some alert is triggered in data2, whereas it should be pushing the alert regardless of whether it is the first or second Arduino sending it an alert. We cannot figure out why this prioritization is occurring or how to fix it.
We are using the following Python code on the Raspberry Pi.
from socket import *
import select
data = None
timeout = 3 # timeout in seconds
host1 = "192.168.1.101"
host2 = "192.168.1.102"
print ("Connecting to " + host1)
print ("Connecting to " + host2)
port1 = 5000
port2=65432
s1 = socket(AF_INET, SOCK_STREAM)
s2 = socket(AF_INET, SOCK_STREAM)
print ("Socket made")
ready1 = select.select([s1],[],[],timeout)
ready2 = select.select([s2],[],[],timeout)
s1.connect((host1,port1))
s2.connect((host2,port2))
print ("Connection made to " + host1)
print ("Connection made to " + host2)
while(1):
print("Data received")
data1=s1.recv(4096)
data2=s2.recv(4096)
if (len(data2)>0 or len(data1)>0):
print(data1)
print(data2)