I. P2P resource release process
Resource publishing is the process in which a resource owner registers resources to the P2P cloud. It consists of the following three steps.
(1) create a P2P keyword object
(2) create a registration object associated with the specified cloud,
(3) Register
The following code demonstrates how to register "0. text ", and associate the name with Port 6000 of the Local Machine (usually listen to this port at the same time to respond to requests from resource visitors ).
PeerName pn = new PeerName ("test", PeerNameType. Unsecured );
PeerNameRegistration pnr = new PeerNameRegistration (pn, 6000, Cloud. Available );
Pnr. Start ();
2. To withdraw resources that have been published to the cloud, you only need to call the stop method of the registered object. The Code is as follows.
Pnr. Stop ();
3. Resource Discovery is to search for a specified keyword in the cloud, in the following three steps.
(1) create a name object to be searched.
(2) create a name interpreter object.
(3). retrieve this name object from the specified cloud.
• The following Code demonstrates how to retrieve the name "0. test" from all available clouds. This process returns a maximum of 10 qualified records.
PeerName searchname = new PeerName ("0. test ");
PeerNameResolver resolver = new PeerNameResolver ();
PeerNameRecordCollection results = resolver. Resolve (searchname, Cloud. Available, 10 );
4. After retrieving the corresponding resource name by P2P name, if it is saved in the results collection object, you can obtain the resource corresponding to this name using the following methods.
Foreach (PeerNameRecord record in results)
{
// Obtain the resource location
Foreach (IPEndPoint endpoint in record. EndPointCollection)
{
// The peer usually listens to this IPEndPoint, and initiates a connection with it to communicate with it or interact with it.
}
// Obtain the data corresponding to the Resource
Byte [] remoteresource = record. Data;
}
5. To run the code successfully, the XP system must perform a simple configuration, that is, add "Point-to-Point Service" to the network service in "add and delete windows Components ", if it is a windows 7 or later system, no configuration is required.