Hello
I have a situation where I sometimes want to send data between two Pis via ethernet, but sometimes the receiving Pi might not be on. The sending Pi therefore has no connection on the ethernet port. In Python I am using UDP but very often (but not always) when I try to send the UDP data, nothing happens after the .sendto, i.e. the rest of the function doesn't happen, although the script itself doesn't hang, the rest of the script works.
This is how I set up the socket:And here is an example of the function to send the data:So for example if I do Send_Data("ABCD")
then print(dataIP) becomes
message ABCD
message ABCDABCD
message ABCDABCDABCD etc
Any ideas why this is happening? I thought UDP didn't care if data was received or not. Sometimes it actually doesn't "block" and the function works fine, perhaps if I start the script some minutes after the Pi has booted. I guess I could detect if the network is up at some point but then it becomes complicated if the receiving pi gets booted later, I would have to constantly check if it is present somehow.
Any ideas?
I have a situation where I sometimes want to send data between two Pis via ethernet, but sometimes the receiving Pi might not be on. The sending Pi therefore has no connection on the ethernet port. In Python I am using UDP but very often (but not always) when I try to send the UDP data, nothing happens after the .sendto, i.e. the rest of the function doesn't happen, although the script itself doesn't hang, the rest of the script works.
This is how I set up the socket:
Code:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
Code:
def Send_Data(d): dataIP="message,"+d+"\r\n" print(dataIP) sock.sendto(dataIP.encode(), ('192.168.1.255', port)) #192.168.1 is the subnet of the ethernet port. print("This print statement doesn't happen")
then print(dataIP) becomes
message ABCD
message ABCDABCD
message ABCDABCDABCD etc
Any ideas why this is happening? I thought UDP didn't care if data was received or not. Sometimes it actually doesn't "block" and the function works fine, perhaps if I start the script some minutes after the Pi has booted. I guess I could detect if the network is up at some point but then it becomes complicated if the receiving pi gets booted later, I would have to constantly check if it is present somehow.
Any ideas?
Statistics: Posted by jimseng — Mon Aug 19, 2024 6:25 am