tags:

views:

996

answers:

4

Why does LocalEndpoint = 0.0.0.0 at this point? According to the docs it should be the appropiate address selected by the system. Note: This only occurrs 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));

directly after creating the socket and it made no difference. The "problem" machine always returns 0.0.0.0.

Here is the result of an 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 I would expect as a result.

A: 

Try using ((IPEndPoint)s.LocalEndPoint).ToString());

AKoran
Makes no difference. Still get 0.0.0.0
Kevin
Do you have a Firewall Client (e.g. ISA Firewall client) installed?Do you have a multi-homed machine?
EricLaw -MSFT-
A: 

Ok decided to use a workaround. Tested and works. Can anyone think of a reason why doing it this way might not give an accurate IP/Port?

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
Kevin
That should work *if* you have only 1 local endpoint bound to a given remote endpoint.
EricLaw -MSFT-
+1  A: 

I Think that you are getting the correct results for the local endpoint on your socket, from what I tested, thats basically the default ip since "0.0.0.0" basically tells the socket to listen to all network adapter if you listen with that socket. the IPEndpoint.Any property basically equals an ip of "0.0.0.0".

I think your getting the your local ip with the netstat since you are resolving it in that method.

Sebastian Bender
A: 

Hi everyone, we tested it now and IP 0.0.0.0 is returned when "ipconfig -all" windows command line command returns at first place Adapter that is disconnected.

If you "closed / stop" (I dont know what word in English it exactly is, I dont have English Windows) the adapter, you see that it return right IP.