debracey
debracey

Reputation: 6597

Sending data to 127.* : Unreachable Host

I'm trying to send to the loopback address space in a .NET 3.5 application running on Windows XP. The code does a simple:

receiver = new IPEndPoint(IPAddress.Parse("127.0.0.2"), 8000);
sock.SendTo(data, len, SocketFlags.None, receiver);

When I run the code I get an unreachable host socket exception. That seems strange to me because the loopback interface is always around, and it shouldn't generate any unreachable host exceptions.

Windows 7 executes the code just fine, making the problem that much stranger.

So -- if anyone has any tips on getting this working in XP, I'd appreciate it.

EDIT:

Some info:

I'm noticing on XP when I ping 127.0.0.2, the replies come back:

Reply from 127.0.0.1

On Windows 7 the reply comes back from the address I pinged:

Reply from 127.0.0.2

I'm thinking this could be the issue and as such it's not really a programming problem, it's more of a problem with XP itself...

-- Dan

Upvotes: 2

Views: 1587

Answers (4)

Thomas Haller
Thomas Haller

Reputation: 199

On Windows 8.0 x64, the Problem seems still exist, sometimes... i dont know, when the problem exists, and when not, it even appears with fresh booted Windows sometimes... i changed my programm to interprete "localhost" to its local ip 4 address.


    public static IPAddress GetConfiguredIPAddress()
    {
        string serverIPString = System.Configuration.ConfigurationManager.AppSettings["serverIP"];

        if (serverIPString.ToLower() == "localhost")
        {
            System.Net.IPHostEntry localhost = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
            foreach (var item in localhost.AddressList)
            {
                if (item.AddressFamily == AddressFamily.InterNetwork && item.GetAddressBytes()[0] != 127)
                {
                    return item;   
                }
            }
        }

        IPAddress result = null;
        if (IPAddress.TryParse(serverIPString, out result))
        {
            return result;
        }

        //I experienced problems with Upper Case DNS Names, so i change this here.
        //dont know if thats correct, because if its really that issue, 
        //it would have been implemented in the Dns class.
        //Note: This also resolves localhost to 127.0.0.1 if you are not connected to any network.
        IPAddress[] dnsAddresses = System.Net.Dns. GetHostAddresses(serverIPString.ToLower());

        if (dnsAddresses.Length > 0)
        {
            IPAddress foundIP4 = null;
            IPAddress foundOtherAddress = null; //usally ipv6

            foreach (IPAddress ip in dnsAddresses)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    foundIP4 = ip;
                }
                else
                {
                    foundOtherAddress = ip;
                }

            }
            if (foundIP4 != null)
            {
                return foundIP4;
            }
            return foundOtherAddress;
        }

        throw new InvalidOperationException("Unable to get IPAddress for " + serverIPString);
    }

Upvotes: 0

debracey
debracey

Reputation: 6597

I fixed the problem by following the steps mentioned in the answer to this post:

How do you create a virtual network interface on Windows?

The original cause of the problem was my need to send data between two programs:

  • Process A: Must send/receive on port 1234 (I cannot change/refactor this)
  • Process B: Must send/receive from process A, but must use a different interface because ultimately it has to use port 1234.

Since my original plan of using the 127.* address block didn't work right on XP, I settled for the virtual network interface approach. I created two virtual network interfaces:

  • 172.17.1.1/255.255.0.0 (for process A)
  • 172.17.1.2/255.255.0.0 (for process B)

The 172.17 address block was already in use for other stuff, so it worked fine here. Not exactly what I wanted because I'll have to add 2 loopback adapters to the user's PC -- but it gets the job done.

--Dan

Upvotes: 1

spender
spender

Reputation: 120488

Do you have a server listening on port 8000 at address 127.0.0.2 (which is distinct from localhost, despite being a loopback)? If not, it won't connect because there's nothing listening. I suspect on your Win7 box, there's something listening.

netstat -b -n -a

should show you what.

EDIT

I suspect you're running XP SP2:

http://support.microsoft.com/kb/884020 (fix available)

IIRC they removed this problem by SP3

Upvotes: 0

Carl Norum
Carl Norum

Reputation: 225022

Does it work with 127.0.0.1? The RFC says:

127.0.0.0/8 - This block is assigned for use as the Internet host loopback address. A datagram sent by a higher level protocol to an address anywhere within this block should loop back inside the host. This is ordinarily implemented using only 127.0.0.1/32 for loopback, but no addresses within this block should ever appear on any network anywhere [RFC1700, page 5].

That seems to indicate that you shouldn't rely on 127.0.0.2 working, unless you've specifically set something up.

Upvotes: 1

Related Questions