locked
DatagramSocket localhost vs 127.0.0.1

    Question

  • Hi,

    I have a .NET 4.5 server that create and UDP socket and bind it to ip: 127.0.0.1 port: 10000

    Then i have a Metro application that use DatagramSocket to send data to the server, if i call ConnectAsync with localhost the messages don't arrive at the server but DataWriter reports the complete messages was wrote to the network, if i use 127.0.0.1 in the call to ConnectAsync, the messages correctly arrives at the server.

    I would expect that both localhost and 127.0.0.1 have the same behavior, seem like a bug in DatagramSocket.

    Thursday, May 17, 2012 10:12 PM

Answers

  • The Visual Studio 11 Beta automatically adds the loopback exemption while developing apps (for example, to develop a local WCF service and Metro style app on the same box). As Rob mentioned, this will not work in a deployed Metro style app.

    You noted you created a Desktop app listening on an IPv4 address. I highly suspect in your Metro style app, localhost is being resolved to an IPv6 localhost address ::1 so that's why it works when you specify the IPv4 address.

    You can change the Desktop app or service to listen on IPv6 or continue forcing the client to use the IPv4 address. This C# snippet demonstrates how to determine what address you resolved to:

                DatagramSocket ds =  new DatagramSocket();
                
                HostName h = new HostName("localhost");
                //Force to IPv4
                //HostName h = new HostName("127.0.0.1");
                //Force to IPv6
                //HostName h = new HostName("::1");
                await ds.ConnectAsync(h, "11000");
                
                //find out what address we resolved to
                string s = ds.Information.RemoteHostName.CanonicalName;
                
                DataWriter dw = new DataWriter(ds.OutputStream);
                dw.WriteString("This message is for: " + s + "\nHello from a Metro style app!");
                //flush
                await dw.StoreAsync();

    Thanks!


    David Lamb

    • Marked as answer by pepone.onrez Wednesday, June 13, 2012 9:23 AM
    Friday, May 25, 2012 4:14 PM
    Moderator

All replies

  • Since loopback is blocked, neither will succeed in production. Metro style apps cannot connect back to the local system (except to the same process). An exception is created for debugging. See How to configure network isolation capabilities       

    Network isolation and loopback

    Network communications using an IP loopback address cannot be used for interprocess communication (between two different apps) since this is restricted by network isolation. Network communication using an IP loopback address is allowed within an app within the same process for communication purposes.

    For more information on enabling developer access to IP loopback addresses between two different apps, see How to enable loopback and debug network isolation.

    --Rob
    Friday, May 18, 2012 12:38 AM
    Owner
  • In my case i running the metro application from Visual Studio Debugger, shouldn't the exception work in both case?
    Friday, May 18, 2012 2:24 PM
  • The Visual Studio 11 Beta automatically adds the loopback exemption while developing apps (for example, to develop a local WCF service and Metro style app on the same box). As Rob mentioned, this will not work in a deployed Metro style app.

    You noted you created a Desktop app listening on an IPv4 address. I highly suspect in your Metro style app, localhost is being resolved to an IPv6 localhost address ::1 so that's why it works when you specify the IPv4 address.

    You can change the Desktop app or service to listen on IPv6 or continue forcing the client to use the IPv4 address. This C# snippet demonstrates how to determine what address you resolved to:

                DatagramSocket ds =  new DatagramSocket();
                
                HostName h = new HostName("localhost");
                //Force to IPv4
                //HostName h = new HostName("127.0.0.1");
                //Force to IPv6
                //HostName h = new HostName("::1");
                await ds.ConnectAsync(h, "11000");
                
                //find out what address we resolved to
                string s = ds.Information.RemoteHostName.CanonicalName;
                
                DataWriter dw = new DataWriter(ds.OutputStream);
                dw.WriteString("This message is for: " + s + "\nHello from a Metro style app!");
                //flush
                await dw.StoreAsync();

    Thanks!


    David Lamb

    • Marked as answer by pepone.onrez Wednesday, June 13, 2012 9:23 AM
    Friday, May 25, 2012 4:14 PM
    Moderator