Can't add a static port mapping in my c# application
Asked Answered
B

3

7

I'm trying to add new static port mapping in my c# application. Because my application runs as a server and i want its to listen on the port 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");

but it doesn't work!, The exception was the following :

Object reference not set to an instance of an object.

Does anyone can help me please?

This is what i'm doing : http://pietschsoft.com/post/2009/02/05/NET-Framework-Communicate-through-NAT-Router-via-UPnP.aspx

Bora answered 13/2, 2013 at 15:7 Comment(4)
What is mappings? Did you initialize it? Put your full code here..Boisleduc
Would be a good idea to post more code. What object type is mappings, for instance.Burdick
@MuhamadSerawan Paste your code in your question. Not as a comment..Boisleduc
That's amazing - no using directives or entry point!Stave
S
3

You need to set mappings to a new instance:

For example:

var mappings = new List<Mapping>();

Then you can call:

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

From your edit:

upnpnat.StaticPortMappingCollection; // this is your problem.  

The collection is coming back as null. Therefore you cannot add to the collection.

You may have to do:

NATUPNPLib.IStaticPortMappingCollection mappings = new StaticPortMappingCollection();

From Codesleuth's comment:

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

Stave answered 13/2, 2013 at 15:9 Comment(4)
Actually, it seems his code should work. See this articleMisusage
@Misusage - it maybe should but the error indicates that the collection has no instance.Stave
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.Misusage
you cant use it like this: NATUPNPLib.IStaticPortMappingCollection mappings = new StaticPortMappingCollection();. The interface IStaticPortMappingCollection is abstractAikido
A
1

Without more code we would be able to help, but if the exception is on the line you provided, that means that mappings is null and not set.

Check the code before that line to see if you're actually creating and setting the mappings variable.

According to your comment, the object returned by upnpnat.StaticPortMappingCollection is what is null. Check the documentation to make sure you're initializing it correctly.

Alfonsoalfonzo answered 13/2, 2013 at 15:9 Comment(0)
S
1

Just based on your error message which just all we have;

var mappings = new List<Mapping>();
mappings.Add(8000, "TCP", 8000, "192.168.1.100", true, "Local Web Server");

After your edit, maybe using like;

NATUPNPLib.IStaticPortMappingCollection mappings = new StaticPortMappingCollection();

Looks like you forget to use ()

Shanika answered 13/2, 2013 at 15:11 Comment(1)
That did not fix the issue.Pouf

© 2022 - 2024 — McMap. All rights reserved.