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

來源:互聯網
上載者:User
使用C#開發一個簡單的P2P應用    spgoal(轉貼)

出處     http://www.aspcool.com

作者: 劉彥青

 本篇文章討論了一種設計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());
  }
  }
  }
  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.