Fwd: 校园网使用upnp协议进行端口映射来在内网中建立侦听服务器

127 views
Skip to first unread message

Dogsaur

unread,
Sep 15, 2013, 11:23:43 AM9/15/13
to xian...@googlegroups.com
 我尽量长话短说,希望各位拔剑相助。
  在做一个项目,我这边需要建立一个服务器程序与硬件系统和客户端进行通信。考虑到信息完整性要求和自己是初学,决定使用TCP。环境是Windows NT ,我用的是C#.net。
  由于通信这边是难点,我先写了一个即时通信的聊天小程序。写完用的时候,我才想起来,到底连接的时候该填公网IP,还是内网IP? 发现自己之前完全没有考虑到这个问题。

  
  查了资料,发现应该是进行端口映射,将自己的内网IP与公网的某一端口进行映射。这本来是一件很简单的事情,只需要在路由上进行NAT设置就行,但是不论是校园网,还是CMCC,学生肯定是没法设的。

  再想办法,看到可以使用upnp协议来直接在我的代码里进行端口映射,而不用在路由上设置。具体是这样做的,我引用了一个COM,NATUPNP.dll ,使用了其中的UPnPNAT类,代码是这样的
    private static void UPnpNAT()
    { 
        IPAddress LocalIP = Dns.Resolve(Dns.GetHostName()).AddressList[0];
        int LocalPort = 1234;
        Console.WriteLine("内网IP:" + LocalIP.ToString() + ":"+LocalPort.ToString());
        UPnPNAT nat = new UPnPNAT();
        var mappings = nat.StaticPortMappingCollection;
        if (mappings == null)
        {
            Console.WriteLine("没有检测到路由器,或者路由器不支持UPnP功能。");
            return;
        }
        mappings.Add(1234, "TCP",LocalPort, LocalIP.ToString(), true, "Dogsaur's Demo");
        Console.WriteLine("映射成功!");Console.WriteLine("testok");
    }

  局部测试的结果:


  但是却一直无法完成映射,为了确定是不是自己环境的问题,我下载了一个upnp test 的小工具, 来检查配置。


  工具附的各个步骤解决方案里,第七步的解答是这样的:
Test 7 Failures

Problem: Windows is ready to receive UPnP messages, but they are never arriving.  When the test tool runs this test, it requests to map a port on the router (TCP 1345), and checks the routers response, if successful, it then deletes this port mapping.

This is the tricky problem to fix, as there are so many problems that can cause this.  I will list as many as possible. Before doing anything, ensure you are running XP Service Pack 2, and if not, stop here, and go get it...  If you are connecting via a USB device, also forget it.

Windows Problems

  • You have installed another firewall in addition to the Windows firewall, and it's blocking the UPnP messages.  (create a rule for UDP 1900 and TCP 2869)
  • You have installed some other software that is interfering somehow with the UPnP messages.  Norton Internet Security is a good example here.  Many people still fail these tests, and it's down to the Norton/Symantec firewall.  Bottom line:  Remove it, or live without UPnP.
  • You need to add the following optional networking components, in Control Panel, Add/Remove Windows Components, Networking:  "Internet Gateway Device Discovery and Control Client" "UPnP User Interface"

Router Problems

  • Your router does not support UPnP!!  if you have a modem, stop now.  UPnP will NOT work.
  • Your router has a USB connection to your PC.  UPnP will only work in NAT mode, which requires Ethernet connection (I believe).
  • The UPnP service is not enabled on the router side (consult the hardware manufacturers documentation on how to enable it)
  • The router is not broadcasting it's UPnP capabilities often enough.  You may find after a while UPnP starts working, this is why.  Some routers allow you to change the interval the UPnP data is sent, other have it set to stupidly high numbers.]
  • You routers UPnP implementation is broken. Many are.  I have a list of known good and bad configurations here  All you can do, is check to see if your manufacturer has released a firmware update to fix things, or complain to them.
  我现在不明白是学校的路由不支持upnp,还是我的程序有问题。
  如果通过校园网无法实现在内网建立外网能访问的侦听服务器,那有什么比较实际的解决方案吗? 我在用netstat 看了下,包括QQ,Rainmeter这些程序都有一个外网IP,无碍的和internet通信,他们是怎么做到的呢? 


  
  新手初学,希望各位倾情相助,不胜感激。

David Xie

unread,
Sep 15, 2013, 12:13:49 PM9/15/13
to xian...@googlegroups.com
关于你的这个问题, 我记得外网和内网的转换, 只要是靠端口了

比如外网IP是A, 内网是B, 那么你B需要访问一个资源在IP C, 从A出去的时候, 它会开一个端口记录B和这个IP C的关系, 当消息回来后, 它能通过端口知道C这个IP返回的数据, 是给B的, 所以就会把数据给B了

印象中, 大致流程是这样的

-- 
David Xie
Sent with Sparrow

--
 
---
您收到此邮件是因为您订阅了 Google 网上论坛的“GDG Xi'an - Xi'an Google Developer Group”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 xian-gdg+u...@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/groups/opt_out。

xxqonline

unread,
Sep 16, 2013, 5:27:37 AM9/16/13
to xian...@googlegroups.com, xian...@googlegroups.com
翻墙程序,没代理怎么行?


发自我的 iPad

Dogsaur

unread,
Sep 16, 2013, 1:33:02 PM9/16/13
to xian...@googlegroups.com
thx for help :-D
你说的这个过程我明白,相当于在从B->A->C 时 使用了一个端口port 将B--C这个链接暂时映射起来,这样如果路由发现一个到A:port 并且是由C来的链接,就会自动转发到内网ip B。
不过不幸的是,我现在遇到的麻烦是,我希望在这样一个内网环境中建立一个侦听服务器,它是被动的,只有当外网向它发送链接申请时,才会产生回应。我之前也找到了解决方案,就是在路由上设置端口映射,手动将某一端口长期映射到一个内网地址上,这样只要访问这个端口,就会被转接到该内网地址。然而问题是,在校园网这样的“局域网”里,学生肯定是不能有权限在路由上设这个的。
我找到的替代方案就是使用upnp协议在我内网的程序中,向路由发起端口映射的请求,但结果是路由没有应答,现在看可能是路由不支持这个协议,可能是关闭了这项功能。
现在,我已经觉得这样可能行不通,我现在仅仅是想找一个替代的解决方案,帮我实现“在内网中,设立能让外网访问的服务器侦听端”这一目的。
感激不尽。

在 2013年9月16日星期一UTC+8上午12时13分49秒,David Xie写道:

Dogsaur

unread,
Sep 16, 2013, 1:36:53 PM9/16/13
to xian...@googlegroups.com
thx a lot :-D
不过这不能称为翻墙吧, 设代理只是为了让在黑名单里的ip的链接看起来像是代理服务器发出的,从而使一些特定的ip可以通过GFW,校园网不涉及这个吧。

在 2013年9月16日星期一UTC+8下午5时27分37秒,xxqonline写道:

Tigerf

unread,
Sep 16, 2013, 10:03:18 PM9/16/13
to xian...@googlegroups.com

upnp主要是方便家庭网络这样的小且私人的网络环境使用。校园网显然不是这类环境,应该不会支持它。其实,我印象中校园网大家都是有公开IP的,不用Nat才对。

在Nat后面的设备要想建立TCP连接肯定要麻烦些。首先,你需要一台在公网上的服务器协助检测Nat。如果通讯双方有一方是在公网上的,那就可以让在Nat后的一方主动发起连接。如果双方都在Nat后,通常的做法应该是通过服务器转发。也有采取端口猜测的方法的,过程比较复杂,还要看具体的设备是否可行。具体的方法可以在网上搜索。当然,要是有设备经过两次nat那就只能转发了。

Reply all
Reply to author
Forward
0 new messages