使用http.get()时无法绑定到IPv6 localAddress-错误:绑定EINVAL

问题描述 投票:0回答:1

我使用nestat将Ubuntu 20.04服务器配置为具有多个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

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

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

我该怎么调试?

node.js networking ipv6
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.