Muh*_*wan 7 .net c# networking upnp
我正在尝试在我的c#应用程序中添加新的静态端口映射.因为我的应用程序作为服务器运行,我希望它在端口8000上监听.
NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();
NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;
mappings.Add(8000, "TCP", 8000, "192.168.1.100", true, "Local Web Server");
Run Code Online (Sandbox Code Playgroud)
但它不起作用!,例外情况如下:
你调用的对象是空的.
有人能帮帮我吗?
这就是我正在做的:http://pietschsoft.com/post/2009/02/05/NET-Framework-Communicate-through-NAT-Router-via-UPnP.aspx
您需要设置到新实例的映射:
例如:
var mappings = new List<Mapping>();
Run Code Online (Sandbox Code Playgroud)
然后你可以调用:
mappings.Add(8000, "TCP", 8000, "192.168.1.100", true, "Local Web Server");
Run Code Online (Sandbox Code Playgroud)
根据您的编辑:
upnpnat.StaticPortMappingCollection; // this is your problem.
Run Code Online (Sandbox Code Playgroud)
该集合返回为空。因此您无法添加到集合中。
你可能需要这样做:
NATUPNPLib.IStaticPortMappingCollection mappings = new StaticPortMappingCollection();
Run Code Online (Sandbox Code Playgroud)
来自 Codesleuth 的评论:
我相信答案是他的路由器没有配置为 UPnP 网关,或者他没有权限。如果这两种情况之一为 true,则 StaticPortMappingCollection 为 null。我建议您将其编辑到您的答案中,因为无论如何您已经找到了错误的正确原因。首先检查 null 是处理错误的唯一方法
| 归档时间: |
|
| 查看次数: |
1909 次 |
| 最近记录: |