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

前端 未结 3 1422
栀梦
栀梦 2021-01-18 05:00

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.

NATUPNPLi         


        
3条回答
  •  余生分开走
    2021-01-18 05:22

    You need to set mappings to a new instance:

    For example:

    var mappings = new List();
    

    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

提交回复
热议问题