C # UPnP

Source: Internet
Author: User

Get host Name
var name = Dns.gethostname ();
Console.WriteLine ("User:" + name);
From the current host name to resolve the IP address, filter IPV4 address is the Local intranet IP address.
var IPv4 = dns.gethostentry (name). Addresslist.where (i = i.addressfamily = = addressfamily.internetwork). FirstOrDefault ();
Console.WriteLine ("Intranet IP:" + IPv4);
Console.WriteLine ("Set UPnP");
UPnP binding information
var eport = 8733;
var iport = 8733;
var description = "MGen test";
To create a COM type
var upnpnat = new Upnpnat ();
var mappings = Upnpnat. Staticportmappingcollection;
Error judgment
if (mappings = = null)
{
Console.WriteLine ("The router is not detected, or the router does not support UPnP functionality.") ");
Return
}
Add previous IPv4 variables (intranet IP), internal ports, and external ports
Mappings. ADD (Eport, "TCP", Iport, IPv4. ToString (), True, description);

Console.WriteLine ("External port: {0}", eport);
Console.WriteLine ("Internal port: {0}", Iport);

External IP variables
String Eip;
Regular
var regex = @ "\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b";
using (var webclient = new WebClient ())
{
var rawres = webclient. Downloadstring ("http://checkip.dyndns.org/");
EIP = Regex.match (Rawres, Regex). Value;
}
Console.WriteLine ("Extranet IP:" + EIP);

C # UPnP

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.