Ruby UDP在本地机器上两个实例之间的通信

发布于 2025-02-02 11:46:50 字数 1721 浏览 2 评论 0 原文

为了测试目的,我想通过在两个不同端口上使用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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乱世争霸 2025-02-09 11:46:50

值得庆幸的是,我解决了这个问题。

实例1(像往常一样发送和接收):

@to_machconn = UDPSocket.new
  
@from_machconn = UDPSocket.new
@from_machconn.bind("localhost", 9093) 

Receiving from Instance 2:
get=@from_machconn.recvfrom(65)  

Sending to Instance 2:
@to_machconn.send(data,0,"127.0.0.1",9092)

实例2:


@from_mach = UDPSocket.new
@from_mach.bind("localhost", 9092)

@to_mach = UDPSocket.new

Receiving from Instance 1:
data=@from_mach.recvfrom_nonblock(512)


Sending to instance 1:
@to_mach.send(data,0,"127.0.0.1",9093)
Receiving from Instance 1


我的错:我至少在这些端口上必须必须在这些端口上进行“连接”或“绑定”,现在被排除在“连接”和“绑定” ”。谢谢!

Thankfully i solved the issue.

Instance 1 (sending and receiving as usual):

@to_machconn = UDPSocket.new
  
@from_machconn = UDPSocket.new
@from_machconn.bind("localhost", 9093) 

Receiving from Instance 2:
get=@from_machconn.recvfrom(65)  

Sending to Instance 2:
@to_machconn.send(data,0,"127.0.0.1",9092)

Instance 2:


@from_mach = UDPSocket.new
@from_mach.bind("localhost", 9092)

@to_mach = UDPSocket.new

Receiving from Instance 1:
data=@from_mach.recvfrom_nonblock(512)


Sending to instance 1:
@to_mach.send(data,0,"127.0.0.1",9093)
Receiving from Instance 1


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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文