For testing purposes i’d like to establish a connection between two Ruby instances on localhost by using Udp on two different Ports, where both instances could send and receive to each other.
Instance 1 has being set up in this way:
@to_machconn = UDPSocket.new @to_machconn.connect("localhost", 9092) @from_machconn = UDPSocket.new @from_machconn.connect("localhost", 9093)
@from_mach = UDPSocket.new @from_mach.bind("localhost", 9092) @to_mach = UDPSocket.new @to_mach.bind("localhost", 9093)```
Communication between Instance 1 (Sender) to Instance 2 (Receiver) works without problems by using
begin data=@from_mach.recvfrom_nonblock(512) rescue end
but the other way around by using
begin # emulate blocking recvfrom data=@from_machconn.recvfrom_nonblock(65) rescue IO::WaitReadable IO.select([@from_machconn]) retry end
won’t work, after ctrl+c on Instance 1 will produce a
/usr/lib/ruby/3.0.0/socket.rb:1272:in `__recvfrom_nonblock': Resource temporarily unavailable - recvfrom(2) would block (IO::EAGAINWaitReadable)
tcpdump -i lo -n udp port 9092 gives me
IP 127.0.0.1.57895 > 127.0.0.1.9092: UDP, length 265
tcpdump -i lo -n udp port 9093
IP 127.0.0.1.9093 > 127.0.0.1.9093: UDP, length 65
Communication between two instances with only one socket involved won´t work either, the result will be the same.
My theory is that sending to 9093 from instance 2 causes somewhat like a loop, where the message which has been delivered by Instance 2 is immediate catched again by Instance 2, so it won’t get “through” to Instance 1, but if so, why?
Probably Binding? If yes, how should especially the Receive-Command on instance 1 be formed? Or has both sides once to used “bind” and “connect” for each port (on the other instance the other way around)?
I’m no Network expert – unfortunaly..