Ruby UDP 服务器/客户端测试失败

发布于 2024-08-11 07:35:23 字数 1120 浏览 2 评论 0原文

我正在尝试使用 Ruby 设置一个简单的 UDP 客户端和服务器。代码如下所示:

require 'socket.so'

class UDPServer
  def initialize(port)
    @port = port
  end

  def start
    @socket = UDPSocket.new
    @socket.bind(nil, @port) # is nil OK here?
    while true
      packet = @socket.recvfrom(1024)
      puts packet
    end
  end
end

server = UDPServer.new(4321)
server.start

这是客户端:

require 'socket.so'

class UDPClient
  def initialize(host, port)
    @host = host
    @port = port
  end

  def start
    @socket = UDPSocket.open
    @socket.connect(@host, @port)
    while true
      @socket.send("otiro", 0, @host, @port)
      sleep 2
    end
  end
end

client = UDPClient.new("10.10.129.139", 4321) # 10.10.129.139 is the IP of UDP server
client.start

现在,我有两台运行 Linux 的 VirtualBox 机器。他们在同一个网络中,可以互相ping通。

但是当我在机器 A 上启动 UDP 服务器,然后尝试在机器 BI 上运行 UDP 客户端时,出现以下错误:

client.rb:13:in `send': Connection refused - sendto(2) (Errno::ECONNREFUSED)

我怀疑错误出在服务器上的绑定方法中。我不知道应该在那里指定哪个地址。我在某处读到您应该使用 LAN/WAN 接口的地址,但我不知道如何获取该地址。

谁能帮我解决这个问题吗?

I am trying to setup a simple UDP client and server using Ruby. The code looks like this:

require 'socket.so'

class UDPServer
  def initialize(port)
    @port = port
  end

  def start
    @socket = UDPSocket.new
    @socket.bind(nil, @port) # is nil OK here?
    while true
      packet = @socket.recvfrom(1024)
      puts packet
    end
  end
end

server = UDPServer.new(4321)
server.start

This is the client:

require 'socket.so'

class UDPClient
  def initialize(host, port)
    @host = host
    @port = port
  end

  def start
    @socket = UDPSocket.open
    @socket.connect(@host, @port)
    while true
      @socket.send("otiro", 0, @host, @port)
      sleep 2
    end
  end
end

client = UDPClient.new("10.10.129.139", 4321) # 10.10.129.139 is the IP of UDP server
client.start

Now, I have two VirtualBox machines running Linux. They are in the same network, they can ping to each other.

But when I start the UDP server on machine A, and then try to run the UDP client on machine B I get the following error:

client.rb:13:in `send': Connection refused - sendto(2) (Errno::ECONNREFUSED)

I suspect that the error is in the bind method on the server. I don't know which address I should specify there. I read somewhere that you should use the address of your LAN/WAN interface, but I don't how to obtain that address.

Can anyone help me with this one?

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

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

发布评论

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

评论(2

泪之魂 2024-08-18 07:35:23

您的主机参数nil被理解为localhost,因此外部机器将无法连接到该套接字。试试这个:

@socket.bind('', @port) # '' ==> INADDR_ANY

来自 Socket 的文档:

host 是主机名或地址字符串(IPv4 的点分十进制,或者
IPv6 的十六进制字符串)
返回信息。零也是
允许,其含义取决于标志,
见下文。

...

Socket::AI_PASSIVE:设置后,如果主机为零,则“任何”地址将为
返回,Socket::INADDR_ANY 或 0
IPv4、“0::0”或“::”表示 IPv6。这
地址适合服务器使用
这将绑定他们的套接字并执行
被动聆听,因此得名
旗帜。否则本地或环回
地址将被返回,这是
对于 IPv4 为“127.0.0.1”,对于 IPv4 为“::1”
IPv6

Your host parameter nil is understood as localhost, so an external machine won't be able to connect to that socket. Try this instead:

@socket.bind('', @port) # '' ==> INADDR_ANY

From the docs for Socket:

host is a host name or an address string (dotted decimal for IPv4, or a
hex string for IPv6) for which to
return information. A nil is also
allowed, its meaning depends on flags,
see below.

....

Socket::AI_PASSIVE: when set, if host is nil the ‘any’ address will be
returned, Socket::INADDR_ANY or 0 for
IPv4, "0::0" or "::" for IPv6. This
address is suitable for use by servers
that will bind their socket and do a
passive listen, thus the name of the
flag. Otherwise the local or loopback
address will be returned, this is
"127.0.0.1" for IPv4 and "::1’ for
IPv6

乖乖兔^ω^ 2024-08-18 07:35:23

服务器中的 @socket.bind("10.10.129.139", @port) 不起作用吗?

编辑:

通常一台机器上可以有多个网络接口(WLAN、LAN,..)。它们都有不同的 IP 地址,因此您必须将一台服务器至少绑定到一个主机地址。

Is @socket.bind("10.10.129.139", @port) in the server not working?

Edit:

Usually you could have multiple network interfaces on one machine (WLAN, LAN, ..). They all have different IP addresses, so you have to bind a server to at least one host address.

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