C # Socket.LocalEndPoint returns 0.0.0.0 on some machines
Why LocalEndpoint = 0.0.0.0 at the moment? According to the documents, this should be the appropriate address chosen by the system. Note. This only happens on some machines. Most machines return the IP address I would expect. Is this a bug in .NET?
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
I have also tried doing
s.Bind(new IPEndPoint(IPAddress.Any, 0));
immediately after the socket was created, and it didn't make any difference. The problem machine always returns 0.0.0.0.
Here is the result of ipconfig / all
Windows IP Configuration
Host Name . . . . . . . . . . . . : andrepc
Primary Dns Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Unknown
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
Ethernet Adapter Local Area Connection 2:
Media State . . . . . . . . . . . : Media disconnected
Description . . . . . . . . . . . : VIA VT6105 Rhine Fast Ethernet Adapter
Physical Address. . . . . . . . . : 00-30-18-67-A0-EB
Ethernet Adapter Local Area Connection:
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 Would be the IP that I would expect in the result.
a source to share
Ok decided to use a workaround. Tested and working. Can anyone think of a reason why this can't give the exact IP / port this way?
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
a source to share
I think you are getting correct results for the local endpoint on your socket, from what I tested, mostly by default, since "0.0.0.0" basically tells the socket to listen on the entire network adapter if you are listening on that socket. the IPEndpoint.Any property is basically ip "0.0.0.0".
I think you are getting your local ip with netstat as you are allowing it in this method.
a source to share
we've tested it now and IP 0.0.0.0 is returned when the command line "ipconfig -all" returns the adapter disabled first .
If you "closed / stopped" (I don't know which word in English it is exactly, I don't have an English Windows) adapter, you will see that it returns the correct IP.