为了测试目的,我想通过在两个不同端口上使用UDP在Local主持的两个Ruby实例之间建立连接,这两个实例都可以相互发送和接收。
实例1 已经以这种方式设置:
@to_machconn = UDPSocket.new
@to_machconn.connect("localhost", 9092)
@from_machconn = UDPSocket.new
@from_machconn.connect("localhost", 9093)
实例2:
@from_mach = UDPSocket.new
@from_mach.bind("localhost", 9092)
@to_mach = UDPSocket.new
@to_mach.bind("localhost", 9093)```
实例1(发件人)之间的通信到实例2(接收器)(接收者)使用
实例1, 无问题
@to_machconn.send(data,0,"127.0.0.1",9092)
实例2 ,
begin
data=@from_mach.recvfrom_nonblock(512)
rescue
end
但通过使用
实例2
@to_mach.send(data,0,"127.0.0.1",9093)
实例1
begin # emulate blocking recvfrom
data=@from_machconn.recvfrom_nonblock(65)
rescue
IO::WaitReadable
IO.select([@from_machconn])
retry
end
在实例1 将产生一个
/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端口9092给我
IP 127.0.0.1.57895 > 127.0.0.1.9092: UDP, length 265
tcpdump -i lo -n udp端口9093
IP 127.0.0.1.9093 > 127.0.0.1.9093: UDP, length 65
在两个实例之间的通信9093仅涉及一个插座的两个实例之间的通信也不会,结果将是结果。相同的。
我的理论是,从实例2发送到9093的原因有点像一个循环,其中已通过实例2立即捕获了已通过实例2立即捕获的消息,因此它不会“通过”到实例1,但是如果是这样。 , 为什么?
可能具有约束力?如果是,则应特别尤其是在实例1上形成接收命令?还是双方都用来使用“绑定”和“连接”每个端口(另一方面是另一个实例)?
我不是网络专家 - 不幸..
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)
Instance 2:
@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
Instance 1
@to_machconn.send(data,0,"127.0.0.1",9092)
Instance 2
begin
data=@from_mach.recvfrom_nonblock(512)
rescue
end
but the other way around by using
Instance 2
@to_mach.send(data,0,"127.0.0.1",9093)
Instance 1
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..
发布评论
评论(1)
值得庆幸的是,我解决了这个问题。
实例1(像往常一样发送和接收):
实例2:
我的错:我至少在这些端口上必须必须在这些端口上进行“连接”或“绑定”,现在被排除在“连接”和“绑定” ”。谢谢!
Thankfully i solved the issue.
Instance 1 (sending and receiving as usual):
Instance 2:
My fault: I throught that at least a "connect" or "bind" to a socket should be mandatory on those Ports, which are now excluded from "connect" and "bind". Thanks!