【问题标题】:NetCore IPV6 socket connection fails on linux-armlinux-arm 上的 NetCore IPV6 套接字连接失败
【发布时间】:2020-03-26 20:08:00
【问题描述】:

我正在尝试在 linux-arm 平台 (raspberry pi) 上与 net-core 3.0 建立 IPV6 套接字连接。

当我尝试将套接字绑定到本地以太网适配器时,抛出异常 ((22): Invalid argument [fe80::211c:bf90:fbbf:9800]:5400)。

当我在我的 Windows 开发机器上尝试相同的操作(使用不同的链接本地 ip)时,一切正常。 IPV4 套接字连接也可以在我的 Windows 开发机器和目标 linux-arm 平台上进行。

到源代码: 我以microsoft的socket示例为基础,将IPV4改为IPV6地址。 在“Bind”方法之后抛出异常。

这是客户端代码:

  //definet the target endpoint
    IPAddress ipAddress;
    IPAddress.TryParse("fe80::211c:bf90:fbbf:9800", out ipAddress);
    IPEndPoint remoteEP = new IPEndPoint(ipAddress, 5400);

    // Create a TCP/IP  socket.  
    Socket sender = new Socket(ipAddress.AddressFamily ,SocketType.Stream, ProtocolType.Tcp);

    //bind to the local network interface
    IPAddress localIp;
    IPAddress.TryParse("fe80::833:e68b:32ee:4c39", out localIp); 
    EndPoint localEndPoint = new IPEndPoint(IPAddress.IPv6Any, 0);
    sender.Bind(localEndPoint);

    // Connect the socket to the remote endpoint. Catch any errors.  
    try
    {
      sender.Connect(remoteEP);

      Console.WriteLine("Socket connected to {0}",
          sender.RemoteEndPoint.ToString());

【问题讨论】:

  • 您在哪里提供区域 ID?链路本地地址需要一个区域 ID 来确定应该使用的接口,因为所有接口都使用相同的链路本地网络。
  • 嗯,好点。基本上这是我的第一个 ipv6 工作,所以我对所有设置都不太熟悉(看起来)。那么,我是否必须将本地区域 ID 传递给远程链接本地的地址?为什么这在 Windows 和 linux 平台上有所不同? windows是否有更智能的方式来确定网卡?

标签: sockets .net-core ipv6


【解决方案1】:

Ron 的输入实际上是缺失的部分。因此,目标端点 IpAddress 必须提供 ScopeId (NIC Nr)。

 //definet the target endpoint
    IPAddress ipAddress;
    IPAddress.TryParse("fe80::211c:bf90:fbbf:9800", out ipAddress);
    ipAddress.ScopeId = scopeId;
    IPEndPoint remoteEP = new IPEndPoint(ipAddress, 5400);

以第一个链接本地地址的范围ID为例,可以使用此代码:

private static long GetScopeIdForHostLinkLocal()
    {
      IPAddress firstLinkLocal = null;
      var info = NetworkInterface.GetAllNetworkInterfaces();

      foreach (NetworkInterface nic in info)
      {
        var ipProps = nic.GetIPProperties();
        var uniAddresses = ipProps.UnicastAddresses;
        foreach (UnicastIPAddressInformation addressInfo in uniAddresses)
        {
          if (addressInfo.Address.IsIPv6LinkLocal)
          {
            firstLinkLocal = addressInfo.Address;
            break;
          }
        }

        if (firstLinkLocal != null)
        {
          break;
        }
      }

      if (firstLinkLocal != null)
      {
        return firstLinkLocal.ScopeId;
      }
      else
      {
        return -1;
      }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    相关资源
    最近更新 更多