使用C 開發乙個簡單的P2P應用

2022-03-24 19:59:40 字數 1431 閱讀 2247

使用c#開發乙個簡單的p2p應用

by oraige 發表於 2005-12-25 12:18:00

c#語言由於其對網路功能良好的支援,特別是內建地支援tcplistener和tcpclient這二個類,使得利用它開發p2p應用程式變得非常容易。下面就是乙個使用c#開發的p2p應用的例子:

public mytcplistener(int port) : base(port)

public void stopme() }

} public class transfer

else

this.tcpl = new mytcplistener(port);

} public void transfershutdown()

public void listenforpeers()

reader.close();

datastream.flush();

datastream.close();

} }

catch(socketexception ex) }

public void downloadtoclient(string server, string remotefilename, string localfilename)

else

// 嘗試與伺服器連線

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)

tcpc.close();

w.close();

fs.close();

} catch(exception ex) }

} }

使用C 開發乙個簡單的P2P應用

當客戶端a想要查詢p2p網路上其他客戶端提供共享的檔案時,系統會執行下面的操作 客戶端a以自己的使用者名稱登入到索引伺服器上。客戶端a向伺服器註冊自己想提供給其他使用者共享的檔案,以便其他使用者能夠查詢到這些檔案。客戶端a向伺服器發出申請,查詢與一定的輸入模式相匹配的檔案。索引伺服器在其資料庫中搜尋...

使用C 開發乙個簡單的P2P應用

當客戶端a想要查詢p2p網路上其他客戶端提供共享的檔案時,系統會執行下面的操作 客戶端a以自己的使用者名稱登入到索引伺服器上。客戶端a向伺服器註冊自己想提供給其他使用者共享的檔案,以便其他使用者能夠查詢到這些檔案。客戶端a向伺服器發出申請,查詢與一定的輸入模式相匹配的檔案。索引伺服器在其資料庫中搜尋...

使用C 開發乙個簡單的P2P應用

使用c 開發乙個簡單的p2p應用 出處 p2p中國 pp c 語言由於其對網路功能良好的支援,特別是內建地支援tcplistener和tcpclient這二個類,使得利用它開發p2p應用程式變得非常容易。下面就是乙個使用c 開發的p2p應用的例子 public mytcplistener int p...