使用C#開發一個簡單的P2P應用


作者: 劉彥青

本篇文章討論了一種設計P2P網絡應用程序的簡單方法。 

  盡管有許多P2P網絡不需要索引服務器或中央服務器,各客戶機之間可以互相直接通訊,但下面的圖1還是顯示了P2P網絡的基本工作原理,一般來說,P2P概念中包含一台中央索引服務器,這台服務器並不存儲有任何文件,它只存儲有登錄到該網絡上的所有用戶的信息、客戶端的IP地址以及用戶提供的供共享的文件,客戶機和服務器使用簡單的命令通過報路連接進行通訊。 

  當客戶端A想要查找P2P網絡上其他客戶端提供共享的文件時,系統會執行下面的操作: 

   ·客戶端A以自己的用戶名登錄到索引服務器上。 

   ·客戶端A向服務器注冊自己想提供給其他用戶共享的文件,以便其他用戶能夠查找到這些文件。 

   ·客戶端A向服務器發出申請,查找與一定的輸入模式相匹配的文件。 

   ·索引服務器在其數據庫中搜索給定的文件名,並將搜索到的如下的結果返回給客戶端A: 

    ·提供該文件的客戶端,例如客戶端B。 

    ·該用戶的IP地址。 

    ·它搜索到的文件名。 




  一旦客戶端A選擇了下載選項,客戶端A就使用搜索返回的IP地址與客戶端B建立連接。 

   ·一旦成功地建立起一個連接,就可以通知對方開始發送文件了。 

   ·下載完成后,應當向索引服務器注冊你得到的共享文件的拷貝。 

  這樣的P2P網絡可以用來共享任何類型的文件,它既可以用在局域網上,也可以作在互聯網上。

                       
C#語言由於其對網絡功能良好的支持,特別是內置地支持TCPListener和TCPClient這二個類,使得利用它開發P2P應用程序變得非常容易。下面就是一個使用C#開發的P2P應用的例子: 

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將處於阻塞狀態 
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[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 

// 缺省的端口號,可以設置為使用的端口號 
port = 8081; 



// 嘗試與服務器連接 
IPHostEntry IPHost = Dns.Resolve(server); 
string []aliases = IPHost.Aliases; 
IPAddress[] addr = IPHost.AddressList; 

IPEndPoint ep = new IPEndPoint(addr[0], port); 
tcpc.Connect(ep); 

// 獲得流對象 
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); 

// 讀取流對象,並將其轉換為ASCII碼 
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()); 



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM