C # Implementation of UPNP operations for P2P communication

Source: Internet
Author: User

Mainly relies on COM interoperability to achieve

Add COM reference using UPNP 1.0 Type Library npnp. dll

It encapsulates UPNP operations.

1. Supported custom types

 

Public enum ProtocolType
{
UDP,
TCP
}

/// <Summary>
/// Port ing information
/// </Summary>
Public class PortMappingInfo
{
Public string InternalIP;
Public int ExternalPort;
Public int InternalPort;
Public ProtocolType type;
Public string Description;
Public PortMappingInfo (string internalIP, int externalPort, int internalPort, ProtocolType type, string description)
{
This. InternalIP = internalIP;
This. ExternalPort = externalPort;
This. InternalPort = internalPort;
This. type = type;
This. Description = description;
}
}

 

2. UPnP class implementation

 

Using System;
Using System. Collections. Generic;
Using System. Collections;
Using System. Text;
Using System. Net;
Using System. Xml;
Using System. IO;
Using System. Net. Sockets;
Using NATUPNPLib;

Namespace Network
{
Public sealed class UPnPNat
{
Private UPnPNATClass _ uPnPNAT;

Public UPnPNat ()
{
Try
{
UPnPNATClass nat = new UPnPNATClass ();
// If (nat. NATEventManager! = Null & nat. StaticPortMappingCollection! = Null)
_ UPnPNAT = nat;
}
Catch {}

If (_ uPnPNAT = null)
Throw new NotSupportedException ("No upnp nat activity can be configured ");
}
/// <Summary>
/// Add Port ing
/// </Summary>
/// <Param name = "ExternalPort"> external port (the port opened by the NAT device of the router) </param>
/// <Param name = "InternalPort"> internal port: local port </param>
/// <Param name = "type"> protocol type UDP or TCP </param>
/// <Param name = "InternalClient"> Intranet IP address or host name </param>
/// <Param name = "enable"> enable </param>
/// <Param name = "description"> description </param>
Public void AddStaticPortMapping (int ExternalPort, int InternalPort, ProtocolType type, string InternalClient, bool enable, string description)
{
_ UPnPNAT. StaticPortMappingCollection. Add (ExternalPort, type. ToString (). ToUpper (), InternalPort, InternalClient, enable, description );
}
/// <Summary>
/// Remove port ing
/// </Summary>
/// <Param name = "ExternalPort"> external port number </param>
/// <Param name = "type"> protocol type </param>
Public void RemoveStaticPortMapping (int ExternalPort, ProtocolType type)
{
_ UPnPNAT. StaticPortMappingCollection. Remove (ExternalPort, type. ToString (). ToUpper ());
}
Public UPnPNATClass UPnPNAT
{
Get {return _ uPnPNAT ;}
}

/// <Summary>
/// Obtain port ing information
/// </Summary>
Public PortMappingInfo [] PortMappingsInfos
{
Get
{
System. Collections. ArrayList portMappings = new ArrayList ();
Int count = _ uPnPNAT. StaticPortMappingCollection. Count;
IEnumerator enumerator = _ uPnPNAT. StaticPortMappingCollection. GetEnumerator ();
Enumerator. Reset ();
For (int I = 0; I <count; I ++)
{
IStaticPortMapping mapping = null;
Try
{
If (enumerator. MoveNext ())
Mapping = (IStaticPortMapping) enumerator. Current;
}
Catch {}
If (mapping! = Null)
PortMappings. Add (new PortMappingInfo (mapping. InternalClient, mapping. ExternalPort, mapping. InternalPort, mapping. Protocol = "TCP "? ProtocolType. TCP: ProtocolType. UDP, mapping. Description ));
}
PortMappingInfo [] portMappingInfos = new PortMappingInfo [portMappings. Count];
PortMappings. CopyTo (portMappingInfos );
Return portMappingInfos;
}
}

}


}

 

Using the port ing function of NAT devices (routers) is an important method for implementing P2P. When UPNP is enabled, an Intranet computer is directly exposed to the Internet.

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.