正在向 127.* 发送数据:无法访问主机

发布于 2024-11-01 02:18:03 字数 781 浏览 5 评论 0原文

我正在尝试发送到在 Windows XP 上运行的 .NET 3.5 应用程序中的环回地址空间。该代码执行简单:

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

当我运行代码时,我收到无法访问的主机套接字异常。这对我来说似乎很奇怪,因为环回接口始终存在,并且它不应该生成任何无法访问的主机异常。

Windows 7 可以很好地执行代码,这使得问题变得更加奇怪。

所以——如果有人有任何关于在 XP 中运行此功能的提示,我将不胜感激。

编辑:

一些信息:

  • 有东西正在监听 127.0.0.2,我的 netstat 显示:
  • UDP 127.0.0.2:8000 : 5824

  • 我正在运行 XP SP3,并且我的测试计算机上没有防火墙

我在 ping 时注意到 XP 上127.0.0.2,回复返回:

Reply from 127.0.0.1

在 Windows 7 上,回复从我 ping 的地址返回:

Reply from 127.0.0.2

我认为这可能是问题所在,因此它并不是真正的编程问题,更多的是 XP 本身的问题。 ..——

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:

  • Something is listening on 127.0.0.2, my netstat shows:
  • UDP 127.0.0.2:8000 : 5824

  • I am running XP SP3, and there is no firewall on my test machine

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

墨落成白 2024-11-08 02:18:03

它可以与127.0.0.1一起使用吗? RFC 说:

127.0.0.0/8 - 该块被分配用作互联网主机
环回地址。由更高层协议发送到一个数据报
该块内任何位置的地址都应循环回主机内部。
这通常仅使用 127.0.0.1/32 进行环回来实现,
但该块内的地址不应出现在任何网络上
任何地方[RFC1700,第 5 页]

这似乎表明您不应该依赖 127.0.0.2 工作,除非您专门设置了某些内容。

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.

这个俗人 2024-11-08 02:18:03

我按照这篇文章的答案中提到的步骤解决了问题:

如何在 Windows 上创建虚拟网络接口?

问题的最初原因是我需要在两个程序之间发送数据:

  • 进程 A:必须在端口 1234 上发送/接收(我无法更改/重构)
  • 进程 B:必须从进程 A 发送/接收,但必须使用不同的接口,因为最终它必须使用端口 1234。

由于我最初使用 127.* 地址块的计划在 XP 上无法正常工作,所以我选择虚拟网络接口方法。我创建了两个虚拟网络接口:

  • 172.17.1.1/255.255.0.0(用于进程 A)
  • 172.17.1.2/255.255.0.0(用于进程 B)

172.17 地址块已用于其他内容,因此在这里工作正常。这并不完全是我想要的,因为我必须向用户的 PC 添加 2 个环回适配器 - 但它可以完成工作。

- 担

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

余生共白头 2024-11-08 02:18:03

您是否有一台服务器正在侦听地址 127.0.0.2 处的端口 8000(这与 localhost 不同,尽管是环回)?如果没有,它将无法连接,因为没有任何东西在监听。我怀疑你的Win7盒子上有东西在监听。

netstat -b -n -a

应该告诉你什么。

编辑

我怀疑您正在运行 XP SP2:

http://support.microsoft.com/kb/884020(可用修复)

IIRC 他们通过 SP3 消除了此问题

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

演出会有结束 2024-11-08 02:18:03

在 Windows 8.0 x64 上,问题似乎仍然存在,有时......
我不知道,问题何时存在,何时不存在,有时甚至会在新启动的 Windows 中出现......
我更改了我的程序以将“localhost”解释为其本地 ip 4 地址。


    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);
    }

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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文