【问题标题】:Can't bind to IPv6 localAddress while using http.get() - Error: bind EINVAL使用 http.get() 时无法绑定到 IPv6 localAddress - 错误:绑定 EINVAL
【发布时间】:2020-09-30 06:46:16
【问题描述】:

我将我的 Ubuntu 20.04 服务器配置为使用 nestat 拥有多个 IPv6 地址,如下所示:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: yes
      dhcp6: no
      addresses:
        - "xxxx:xxxx:xxxx:xxxx::1/64"
        - "xxxx:xxxx:xxxx:xxxx::2/64"
      gateway6: "xxxx::1"
      nameservers:
        search: [xxxx.xxxx.com]
        addresses:
          - xxxx:xxxx::6
          - xxxx:xxxx::7

我可以在ifconfig 的输出中看到这些地址,当我在同一台服务器上使用ping6 对它们执行 ping 操作时,它可以正常工作。 现在我正在尝试使用localAddress 绑定到这些 IPv6 地址并发送 HTTP GET 请求:

const http = require('http')

const options = {
    host: "xxx.xxx.xxx.xxx",
    port: 3000,
    path: '/',
    family: 6,
    localAddress: 'xxxx:xxxx:xxxx:xxxx::1'
}

const req = http.get(options)

req.on('response', res => {
    res.on('data', chunk => console.log)
})

这会导致以下错误:

Error: bind EINVAL xxxx:xxxx:xxxx:xxxx::1

我了解此错误可能意味着多种情况:

socket已经绑定了一个地址,协议没有 支持绑定新地址;或套接字已关闭。

我可以做些什么来调试这个?

【问题讨论】:

    标签: node.js networking ipv6


    【解决方案1】:

    我解决了这个问题,答案很简单——我连接的服务器根本不支持 IPv6。如果我用 google.com 替换 host 值,一切正常。我希望错误消息更具描述性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-19
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 1970-01-01
      • 2016-04-08
      相关资源
      最近更新 更多