Although many P2P networks do not require indexing servers or central servers and clients can communicate with each other directly, figure 1 below shows the basic working principles of P2P networks. Generally, the P2P concept includes a central Indexing Server, which does not store any files, it only stores information about all users logged on to the network, IP addresses of clients, and files provided by users for sharing, the client and server use simple commands to communicate through the reporting connection.
When Client A wants to find files shared by other clients on the P2P network, the system performs the following operations:
· Client A logs on to the Indexing Server as its username.
· Client A registers with the server the files that he wants to share with other users so that other users can find these files.
· Client A sends an application to the server to search for files that match certain input modes.
· The Indexing Server searches for the given file name in its database and returns the following results to Client:
· The client that provides the file, such as client B.
· The IP address of the user.
· The name of the file to be searched.
Once Client A selects the download option, client A uses the IP address returned by the search to establish A connection with client B.
· Once a connection is established successfully, the recipient can be notified to start sending files.
· After the download is complete, you should register a copy of the shared file with the Index Server.
Such a P2P network can be used to share any type of files. It can be used either on a LAN or on the Internet.
C # language is very easy to develop P2P applications because of its excellent support for network functions, especially the built-in support for TCPListener and TCPClient. The following is an example of a P2P application developed using C:
Public MyTcpListener (int port): base (port)
{
}
Public void StopMe ()
{
If (this. Server! = Null)
{
This. Server. Close ();
}
}
}
Public class Transfer
{
MyTcpListener tcpl;
Public Transfer ()
{
OptionsLoader ol = new OptionsLoader ();
Int port = 8081;
If (ol. Port> 0)
{
Port = ol. Port;
}
Else
{
Port = 8081;
}
This. tcpl = new MyTcpListener (port );
}
Public void TransferShutdown ()
{
Tcpl. StopMe ();
}
Public void ListenForPeers ()
{
Try
{
Encoding ASCII = Encoding. ASCII;
Tcpl. Start ();
While (true)
{
// Accept will be blocked before connection
Socket s = tcpl. AcceptSocket ();
NetworkStream DataStream = new NetworkStream (s );
String filename;
Byte [] Buffer = new Byte [256];
DataStream. Read (Buffer, 0,256 );
Filename = Encoding. ASCII. GetString (Buffer );
StringBuilder sbFileName = new StringBuilder (filename );
StringBuilder sbFileName2 = sbFileName. Replace ("/","//");
FileStream fs = new FileStream (sbFileName2.ToString (), FileMode. Open, FileAccess. Read );
BinaryReader reader = new BinaryReader (fs );
Byte [] bytes = new byte [1, 1024];
Int read;
While (read = reader. Read (bytes, 0, bytes. Length ))! = 0)
{
DataStream. Write (bytes, 0, read );
}
Reader. Close ();
DataStream. Flush ();
DataStream. Close ();
}
}
Catch (SocketException ex)
{
MessageBox. Show (ex. ToString ());
}
}
Public void DownloadToClient (String server, string remotefilename, string localfilename)
{
Try
{
TcpClient tcpc = new TcpClient ();
Byte [] read = new Byte [1024];
OptionsLoader ol = new OptionsLoader ();
Int port = 0;
If (ol. Port> 0)
{
Port = ol. Port;
}
Else
{
// Default port number, which can be set to the used port number
Port = 8081;
}
// Try to connect to the server
IPHostEntry IPHost = Dns. Resolve (server );
String [] aliases = IPHost. Aliases;
IPAddress [] addr = IPHost. AddressList;
IPEndPoint ep = new IPEndPoint (addr [0], port );
Tcpc. Connect (ep );
// Obtain the Stream Object
Stream s = tcpc. GetStream ();
Byte [] B = Encoding. ASCII. GetBytes (remotefilename. ToCharArray ());
S. Write (B, 0, B. Length );
Int bytes;
FileStream fs = new FileStream (localfilename, FileMode. OpenOrCreate );
BinaryWriter w = new BinaryWriter (fs );
// Read the stream object and convert it to ASCII code
While (bytes = s. Read (read, 0, read. Length ))! = 0)
{
W. Write (read, 0, bytes );
Read = new Byte [1024];
}
Tcpc. Close ();
W. Close ();
Fs. Close ();
}
Catch (Exception ex)
{
Throw new Exception (ex. ToString ());
}
}
}
}