加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

无法在我的c#应用程序中添加静态端口映射

发布时间:2020-12-15 07:47:42 所属栏目:百科 来源:网络整理
导读:我正在尝试在我的c#应用程序中添加新的静态端口映射.因为我的应用程序作为服务器运行,我希望它在端口8000上监听. NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPort
我正在尝试在我的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");

但它不起作用!,例外情况如下:

Object reference not set to an instance of an object.

有人可以帮我吗?

这就是我正在做的事:http://pietschsoft.com/post/2009/02/05/NET-Framework-Communicate-through-NAT-Router-via-UPnP.aspx

解决方法

您需要将映射设置为新实例:

例如:

var mappings = new List<Mapping>();

然后你可以打电话:

mappings.Add(8000,"Local Web Server");

从你的编辑:

upnpnat.StaticPortMappingCollection; // this is your problem.

该集合将返回null.因此您无法添加到集合中.

你可能必须这样做:

NATUPNPLib.IStaticPortMappingCollection mappings = new StaticPortMappingCollection();

来自Codesleuth的评论:

I believe the answer is that his router isn’t configured as a UPnP gateway,or that he has no permissions. The StaticPortMappingCollection is null if either of those cases are true. I suggest you edit this into your answer as you’ve got the right reason for the error anyway. Checking for null first is the only way to deal with the error

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读