C#Socket.LocalEndPoint在某些机器上返回0.0.0.0
为什么此时LocalEndpoint=0.0.0.0?根据文档,它应该是系统选择的适当地址。注意:这仅在某些机器上发生。大多数机器返回我期望的IP地址。这是.NET中的错误吗C#Socket.LocalEndPoint在某些机器上返回0.0.0.0,c#,sockets,C#,Sockets,为什么此时LocalEndpoint=0.0.0.0?根据文档,它应该是系统选择的适当地址。注意:这仅在某些机器上发生。大多数机器返回我期望的IP地址。这是.NET中的错误吗 using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { Console.WriteLine("Connecting"); s.Connect("
using (Socket s = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp))
{
Console.WriteLine("Connecting");
s.Connect("www.google.com", 80);
Console.WriteLine("Connected OK");
s.Send(new byte[] { 1 });
Console.WriteLine("Sent Byte OK");
Console.WriteLine("Local EndPoint = " +
s.LocalEndPoint.ToString());
}
//Local EndPoint = 0.0.0.0
我也试过这样做
s.Bind(new IPEndPoint(IPAddress.Any, 0));
直接在创建套接字之后,它没有任何区别。“问题”机器始终返回0.0.0.0
以下是ipconfig/all的结果
Windows IP Configuration
Host Name . . . . . . . . . . . . : andrepc
Primary Dns Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Unknown
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
以太网适配器局域网连接2:
Media State . . . . . . . . . . . : Media disconnected
Description . . . . . . . . . . . : VIA VT6105 Rhine Fast Ethernet Adapter
Physical Address. . . . . . . . . : 00-30-18-67-A0-EB
以太网适配器局域网连接:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Realtek RTL8029 PCI Ethernet Adapter
Physical Address. . . . . . . . . : 00-C0-DF-E7-C9-5D
Dhcp Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
IP Address. . . . . . . . . . . . : 10.0.0.6
Subnet Mask . . . . . . . . . . . : 255.0.0.0
Default Gateway . . . . . . . . . : 10.0.0.2
DHCP Server . . . . . . . . . . . : 10.0.0.2
DNS Servers . . . . . . . . . . . : 10.0.0.2
Lease Obtained. . . . . . . . . . : Wednesday, May 20, 2009 5:39:06 PM
Lease Expires . . . . . . . . . . : Thursday, May 21, 2009 5:39:06 PM
10.0.0.6将是我期望得到的IP。尝试使用((IPEndPoint)s.LocalEndPoint.ToString()) 尝试使用((IPEndPoint)s.LocalEndPoint.ToString()) Ok决定使用变通方法。测试和工作。有人能想出这样做可能无法提供准确IP/端口的原因吗
static void Main(string[] args)
{
try
{
using (Socket s = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp))
{
Console.WriteLine("Connecting");
s.Connect("www.google.com", 80);
Console.WriteLine("Connected OK");
s.Send(new byte[] { 1 });
Console.WriteLine("Sent Byte OK");
Console.WriteLine("Local EndPoint Netstat = " +
GetLocalEndPoint(s));
Console.WriteLine("Local EndPoint LocalEndPoint = " +
((IPEndPoint)s.LocalEndPoint).ToString());
}
}
catch (Exception e)
{
Console.WriteLine("Exception - " + e.Message);
}
}
static string GetLocalEndPoint(Socket s)
{
Console.WriteLine(DateTime.Now.TimeOfDay.ToString() +
" Find IP LocalEndPoint");
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] connections =
properties.GetActiveTcpConnections();
IPEndPoint remoteEP = (IPEndPoint)s.RemoteEndPoint;
foreach (TcpConnectionInformation netstat in connections)
{
if (remoteEP.ToString() == netstat.RemoteEndPoint.ToString())
{
Console.WriteLine(DateTime.Now.TimeOfDay.ToString() +
" Find IP LocalEndPoint OK");
//Use the "Netstat" IP but the socket.LocalEndPoint port
return new IPEndPoint(netstat.LocalEndPoint.Address,
((IPEndPoint)s.LocalEndPoint).Port).ToString();
}
}
return string.Empty;
}
Connecting
Connected OK
Sent Byte OK
09:57:38.5312500 Find IP LocalEndPoint
09:57:38.5312500 Find IP LocalEndPoint OK
Local EndPoint Netstat = 10.0.0.6:1711
Local EndPoint LocalEndPoint = 0.0.0.0:1711
Ok决定使用一种变通方法。测试和工作。有人能想出这样做可能无法提供准确IP/端口的原因吗
static void Main(string[] args)
{
try
{
using (Socket s = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp))
{
Console.WriteLine("Connecting");
s.Connect("www.google.com", 80);
Console.WriteLine("Connected OK");
s.Send(new byte[] { 1 });
Console.WriteLine("Sent Byte OK");
Console.WriteLine("Local EndPoint Netstat = " +
GetLocalEndPoint(s));
Console.WriteLine("Local EndPoint LocalEndPoint = " +
((IPEndPoint)s.LocalEndPoint).ToString());
}
}
catch (Exception e)
{
Console.WriteLine("Exception - " + e.Message);
}
}
static string GetLocalEndPoint(Socket s)
{
Console.WriteLine(DateTime.Now.TimeOfDay.ToString() +
" Find IP LocalEndPoint");
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] connections =
properties.GetActiveTcpConnections();
IPEndPoint remoteEP = (IPEndPoint)s.RemoteEndPoint;
foreach (TcpConnectionInformation netstat in connections)
{
if (remoteEP.ToString() == netstat.RemoteEndPoint.ToString())
{
Console.WriteLine(DateTime.Now.TimeOfDay.ToString() +
" Find IP LocalEndPoint OK");
//Use the "Netstat" IP but the socket.LocalEndPoint port
return new IPEndPoint(netstat.LocalEndPoint.Address,
((IPEndPoint)s.LocalEndPoint).Port).ToString();
}
}
return string.Empty;
}
Connecting
Connected OK
Sent Byte OK
09:57:38.5312500 Find IP LocalEndPoint
09:57:38.5312500 Find IP LocalEndPoint OK
Local EndPoint Netstat = 10.0.0.6:1711
Local EndPoint LocalEndPoint = 0.0.0.0:1711
我认为您在套接字上获得了本地端点的正确结果,从我测试的结果来看,这基本上是默认ip,因为“0.0.0.0”基本上告诉套接字,如果您使用该套接字侦听,那么它将侦听所有网络适配器。IPEndpoint。任何属性基本上等于ip的“0.0.0.0”
我认为您可以使用netstat获取本地ip,因为您正在使用该方法解析它。我认为您在套接字上获得本地端点的正确结果,根据我的测试,这基本上是自“0.0.0.0”以来的默认ip基本上告诉套接字,如果您使用该套接字进行侦听,则它将侦听所有网络适配器。IPEndpoint。任何属性基本上等于ip的“0.0.0.0”
我认为您可以使用netstat获取本地ip,因为您正在使用该方法解析它。我们现在测试了它,当“ipconfig-all”windows命令行命令返回第一个断开连接的适配器时,将返回ip 0.0.0
如果您“关闭/停止”(我不知道它确切是什么英文单词,我没有英文Windows)适配器,您会看到它返回正确的IP。我们现在测试了它,当“ipconfig-all”Windows命令行命令返回位于第一位的适配器时返回IP 0.0.0,该适配器已断开连接
如果您在适配器上“关闭/停止”(我不知道它到底是什么英文单词,我没有英文Windows),您会看到它返回正确的IP。尝试在计算机上的命令控制台中键入“ipconfig/all”。尝试在计算机上的命令控制台中键入“ipconfig/all”。没有区别。仍然获得0.0.0.0您是否安装了防火墙客户端(例如ISA防火墙客户端)?您有多主机计算机吗?没有安装防火墙客户端。我自己也怀疑是ISA防火墙客户端。没什么区别。仍然获得0.0.0.0您是否安装了防火墙客户端(例如ISA防火墙客户端)?您有多主机计算机吗?没有安装防火墙客户端。我自己也怀疑是ISA防火墙客户端。如果只有一个本地端点绑定到给定的远程端点,那么这应该可以工作。如果只有一个本地端点绑定到给定的远程端点,那么这应该可以工作。