插座C#和不同的子网

插座C#和不同的子网,c#,sockets,C#,Sockets,我第一次尝试实现一个套接字应用程序。 当我使用: IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000); localEndPoint IPEndPoint包含“192.168.56.1”,这是我在VirtualBox网络

我第一次尝试实现一个套接字应用程序。 当我使用:

IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
localEndPoint IPEndPoint包含“192.168.56.1”,这是我在VirtualBox网络下的地址。 它应该包含我的本地网络ip(“192.168.1.165”)

我怎么办


我浏览了谷歌,但我找到了一个答案,对不起…

您的虚拟机对外部世界的网络接口一无所知。您只能使用方法()查找您的本地地址。任何其他内容都应该是配置设置。

使用
IPAddress。任何
都可以在所有本地接口上绑定。对于大多数情况,您不需要找到特定的本地IP

请注意,除了一个地址外,您将丢弃所有地址。难怪你只得到一个


没有所谓的本地IP。它是一个集合。

我不喜欢虚拟机。我在安装VirtualBox的机器上。它创建了自己的虚拟网络,但我根本不使用它。哦,我明白了。当你有VirtualBox时,你有多个本地接口,因此有多个本地地址。你仍然可以使用我在回答中提到的方法。然后,您可以获取每个接口的IP属性,以查看哪个接口具有默认网关,通常这就是您要查找的接口。请注意,任何一台机器都可以有零个或任意数量的多个传出IP地址。