C # P2P (1)

Source: Internet
Author: User

 

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. net;
Using system. net. Sockets;
Using system. net. peertopeer;

Namespace consoleapplication4
{
Class Program
{
Static string strmypeername = "myapplicationname ";
Static string strallmypeername = "";
Static int port;
Static peernameregistration;

Static void main (string [] ARGs)
{
Registerpeer ();
Resovename ("0." + strmypeername );
Console. Read ();
}

Public static void getport ()
{
While (true)
{
Port = new random (). Next );
Try
{
Tcplistener listener = new tcplistener (IPaddress. Any, Port );
Listener. Start ();
}
Catch
{
Continue;
}
Break;
}
}

 

 

// Register the PNRP name to the cloud

Public static void registerpeer ()
{
Getport (); // obtain the IP address and port to be connected.

Peername = new peername (strmypeername, peernametype. Unsecured );

// Use peername to create a registration group, and then register peername to the group.

Peernameregistration = new peernameregistration (peername, Port );
Peernameregistration. Comment = "other descriptions of PNRP peer name ";
Peernameregistration. Data = encoding. utf8.getbytes (string. Format ("invalid duration {0}", datetime. Now. tostring ()));
Strallmypeername = peername. tostring ();
Peernameregistration. Start ();
}

Public static void resolverpeer (string mypeername)
{
Peername = new peername ("0." + mypeername );
Peernameresolver resolver = new peernameresolver ();
Peernamerecordcollection pmrcs = resolver. Resolve (peername );
Foreach (peernamerecord PMRC in pmrcs)
{
Foreach (ipendpoint endpoint in PMRC. endpointcollection)
{
Console. writeline (endpoint );
}
}
}

 

/// Parse the peer name

Public static void resovename (string strpeername)
{
Peername mypeer = new peername (strpeername );
Peernameresolver Myres = new peernameresolver ();
Peernamerecordcollection reccoll = Myres. Resolve (mypeer );
Foreach (VAR record in reccoll)
{
Foreach (VAR endp in record. endpointcollection)
{
If (endp. addressfamily. Equals (addressfamily. InterNetwork ))
{
Console. writeline (record. peername. tostring ());
Console. writeline (endp. tostring ());
Console. writeline (encoding. utf8.getstring (record. Data ));
Console. writeline (record. peername. peerhostname );
}
}
}
}

}
}

 

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.