无法使用单声道连接到 ubuntu 上自己的 IPv6 TCP 服务器应用程序 运行
Cant connect to own IPv6 TCP server app running on ubuntu with mono
我的测试基础设施由 3 台机器组成:1 台安装了 windows 10 运行ning hyperv 的物理机和 2 台安装了 ubuntu OS 的虚拟机。所有机器之间都有完整的网络连接(它们相互 ping)。我写了 2 个简单的 C# 程序:TCP 客户端和 TCP 服务器(下面我附上了重现问题的最少代码)。当我 运行 windows 机器上的客户端和其中一台 ubuntu 机器上的服务器时,一切正常。但是,当我尝试 运行 一台 ubuntu 机器上的客户端和另一台 ubuntu 机器上的服务器时,我在客户端收到错误消息:
TCPClientTest.exe Information: 0 : System.Net.Sockets.SocketException (0x80004005): Invalid arguments
at System.Net.Sockets.TcpClient.Connect (System.Net.IPAddress[] ipAddresses, System.Int32 port) [0x000e9] in <59be416de143456b88b9988284f43350>:0
at System.Net.Sockets.TcpClient.Connect (System.String hostname, System.Int32 port) [0x00007] in <59be416de143456b88b9988284f43350>:0
at System.Net.Sockets.TcpClient..ctor (System.String hostname, System.Int32 port) [0x00006] in <59be416de143456b88b9988284f43350>:0
at TCPClientTest.Program.Main (System.String[] args) [0x00002] in :0
DateTime=2016-11-09T21:25:42.4641950Z
TCP 客户端:
TcpClient client = new TcpClient("fe80::3", 15000);
NetworkStream stream = client.GetStream();
int number = stream.ReadByte();
stream.Close();
client.Close();
TCP 服务器:
TcpListener server = new TcpListener(IPAddress.IPv6Any, 15000);
server.Start();
TcpClient client = server.AcceptTcpClient();
NetworkStream stream = client.GetStream();
stream.WriteByte(199);
stream.Close();
client.Close();
Ubuntu 版本:16.04 LTS
单声道版本:4.6 服务版本 0.1 (4.6.1.5)
可能是什么问题?
我找到问题了。 IPv6 link 本地地址与一个名为 scope id 的数字相关联,该数字指定了我们希望用于连接的网络接口。看起来在 Linux 系统中我们必须明确提供此信息(即使使用 ping6 我们也需要这样做)但在 Windows 中没有这样的要求并且根据这篇文章 https://technet.microsoft.com/pl-pl/ms739166 它正在使用Neighbor Discovery 并尝试为我们获取正确的接口。
例如:
在 Windows ping fe80::3
会正常工作,但在 Linux 我们需要做 ping6 -I eth0 fe80::3
或 ping6 fe80::3%2
必须稍微修改 TCP 客户端以遵守范围 ID 并指定它将用于正常工作的网络接口:
IPAddress ipAddress = null;
foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
{
IPInterfaceProperties ipIntProperties = networkInterface.GetIPProperties();
foreach (UnicastIPAddressInformation addr in ipIntProperties.UnicastAddresses)
{
String addressWithoutScopeId = addr.Address.ToString().Split('%')[0];
if (addressWithoutScopeId.Equals("fe80::2"))
{
ipAddress = addr.Address;
break;
}
}
if (ipAddress != null)
break;
}
var endPoint = new IPEndPoint(ipAddress, 0);
TcpClient client = new TcpClient(endPoint);
client.Connect(IPAddress.Parse("fe80::3"), 15000);
NetworkStream stream = client.GetStream();
int number = stream.ReadByte();
stream.Close();
client.Close();
我的测试基础设施由 3 台机器组成:1 台安装了 windows 10 运行ning hyperv 的物理机和 2 台安装了 ubuntu OS 的虚拟机。所有机器之间都有完整的网络连接(它们相互 ping)。我写了 2 个简单的 C# 程序:TCP 客户端和 TCP 服务器(下面我附上了重现问题的最少代码)。当我 运行 windows 机器上的客户端和其中一台 ubuntu 机器上的服务器时,一切正常。但是,当我尝试 运行 一台 ubuntu 机器上的客户端和另一台 ubuntu 机器上的服务器时,我在客户端收到错误消息:
TCPClientTest.exe Information: 0 : System.Net.Sockets.SocketException (0x80004005): Invalid arguments at System.Net.Sockets.TcpClient.Connect (System.Net.IPAddress[] ipAddresses, System.Int32 port) [0x000e9] in <59be416de143456b88b9988284f43350>:0 at System.Net.Sockets.TcpClient.Connect (System.String hostname, System.Int32 port) [0x00007] in <59be416de143456b88b9988284f43350>:0 at System.Net.Sockets.TcpClient..ctor (System.String hostname, System.Int32 port) [0x00006] in <59be416de143456b88b9988284f43350>:0 at TCPClientTest.Program.Main (System.String[] args) [0x00002] in :0 DateTime=2016-11-09T21:25:42.4641950Z
TCP 客户端:
TcpClient client = new TcpClient("fe80::3", 15000);
NetworkStream stream = client.GetStream();
int number = stream.ReadByte();
stream.Close();
client.Close();
TCP 服务器:
TcpListener server = new TcpListener(IPAddress.IPv6Any, 15000);
server.Start();
TcpClient client = server.AcceptTcpClient();
NetworkStream stream = client.GetStream();
stream.WriteByte(199);
stream.Close();
client.Close();
Ubuntu 版本:16.04 LTS
单声道版本:4.6 服务版本 0.1 (4.6.1.5)
可能是什么问题?
我找到问题了。 IPv6 link 本地地址与一个名为 scope id 的数字相关联,该数字指定了我们希望用于连接的网络接口。看起来在 Linux 系统中我们必须明确提供此信息(即使使用 ping6 我们也需要这样做)但在 Windows 中没有这样的要求并且根据这篇文章 https://technet.microsoft.com/pl-pl/ms739166 它正在使用Neighbor Discovery 并尝试为我们获取正确的接口。
例如:
在 Windows ping fe80::3
会正常工作,但在 Linux 我们需要做 ping6 -I eth0 fe80::3
或 ping6 fe80::3%2
必须稍微修改 TCP 客户端以遵守范围 ID 并指定它将用于正常工作的网络接口:
IPAddress ipAddress = null;
foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
{
IPInterfaceProperties ipIntProperties = networkInterface.GetIPProperties();
foreach (UnicastIPAddressInformation addr in ipIntProperties.UnicastAddresses)
{
String addressWithoutScopeId = addr.Address.ToString().Split('%')[0];
if (addressWithoutScopeId.Equals("fe80::2"))
{
ipAddress = addr.Address;
break;
}
}
if (ipAddress != null)
break;
}
var endPoint = new IPEndPoint(ipAddress, 0);
TcpClient client = new TcpClient(endPoint);
client.Connect(IPAddress.Parse("fe80::3"), 15000);
NetworkStream stream = client.GetStream();
int number = stream.ReadByte();
stream.Close();
client.Close();