图老师小编精心整理的使用C#开发一个简单的P2P应用希望大家喜欢,觉得好的亲们记得收藏起来哦!您的支持就是小编更新的动力~
【 tulaoshi.com - 编程语言 】
尽管有许多P2P网络不需要索引服务器或中央服务器,各客户机之间可以互相直接通讯,但下面的图1还是显示了P2P网络的基本工作原理,一般来说,P2P概念中包含一台中央索引服务器,这台服务器并不存储有任何文件,它只存储有登录到该网络上的所有用户的信息、客户端的IP地址以及用户提供的供共享的文件,客户机和服务器使用简单的命令通过报路连接进行通讯。
  当客户端A想要查找P2P网络上其他客户端提供共享的文件时,系统会执行下面的操作:
   客户端A以自己的用户名登录到索引服务器上。
   客户端A向服务器注册自己想提供给其他用户共享的文件,以便其他用户能够查找到这些文件。
   客户端A向服务器发出申请,查找与一定的输入模式相匹配的文件。
   索引服务器在其数据库中搜索给定的文件名,并将搜索到的如下的结果返回给客户端A:
    提供该文件的客户端,例如客户端B。
    该用户的IP地址。
    它搜索到的文件名。
  一旦客户端A选择了下载选项,客户端A就使用搜索返回的IP地址与客户端B建立连接。
   一旦成功地建立起一个连接,就可以通知对方开始发送文件了。
   下载完成后,应当向索引服务器注册你得到的共享文件的拷贝。
  这样的P2P网络可以用来共享任何类型的文件,它既可以用在局域网上,也可以作在互联网上。
  
(图1)
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)
C#语言由于其对网络功能良好的支持,特别是内置地支持TCPListener和TCPClient这二个类,使得利用它开发P2P应用程序变得非常容易。下面就是一个使用C#开发的P2P应用的例子:
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)   1 public MyTcpListener(int port) : base(port)
public MyTcpListener(int port) : base(port)
  2
  3 public void StopMe()
public void StopMe() 
  4
 {
{
  5 if ( this.Server != null )
if ( this.Server != null )
  6
  7 }
}
  8 }
}
  9
 10 public class Transfer
public class Transfer
 11
 {
{
 12 MyTcpListener tcpl;
MyTcpListener tcpl;
 13
 14 public Transfer()
public Transfer()
 15
 {
{
 16 OptionsLoader ol = new OptionsLoader();
OptionsLoader ol = new OptionsLoader(); 
 17 int port = 8081;
int port = 8081;
 18 if (ol.Port  0)
if (ol.Port  0)
 19
 {
{
 20 port = ol.Port;
port = ol.Port;
 21 }
}
 22 else
else
 23
 {
{
 24 port = 8081;
port = 8081;
 25 }
} 
 26
 27 this.tcpl = new MyTcpListener(port);
this.tcpl = new MyTcpListener(port); 
 28 }
}
 29
 30 public void TransferShutdown()
public void TransferShutdown()
 31
 32
 33 public void ListenForPeers()
public void ListenForPeers() 
 34
 {
{ 
 35 try
try
 36
 {
{ 
 37
 38 Encoding ASCII = Encoding.ASCII;
Encoding ASCII = Encoding.ASCII; 
 39
 40
 41 tcpl.Start();
tcpl.Start(); 
 42
 43
 44 while (true)
while (true)
 45
 {
{ 
 46 // 在有连接之前,Accept将处于阻塞状态
// 在有连接之前,Accept将处于阻塞状态
 47 Socket s = tcpl.AcceptSocket();
Socket s = tcpl.AcceptSocket(); 
 48 NetworkStream DataStream = new NetworkStream(s);
NetworkStream DataStream = new NetworkStream(s);
 49
 50 String filename;
String filename;
 51 Byte[] Buffer = new Byte[256];
Byte[] Buffer = new Byte[256];
 52 DataStream.Read(Buffer, 0, 256);
DataStream.Read(Buffer, 0, 256);
 53 filename = Encoding.ASCII.GetString(Buffer);
filename = Encoding.ASCII.GetString(Buffer);
 54 StringBuilder sbFileName = new StringBuilder(filename);
StringBuilder sbFileName = new StringBuilder(filename);
 55 StringBuilder sbFileName2 = sbFileName.Replace("", "");
StringBuilder sbFileName2 = sbFileName.Replace("", "");
 56 FileStream fs = new FileStream(sbFileName2.ToString(), FileMode.Open, FileAccess.Read);
FileStream fs = new FileStream(sbFileName2.ToString(), FileMode.Open, FileAccess.Read); 
 57 BinaryReader reader = new BinaryReader(fs);
BinaryReader reader = new BinaryReader(fs);
 58 byte[] bytes = new byte[1024];
byte[] bytes = new byte[1024];
 59 int read;
int read;
 60 while((read = reader.Read(bytes, 0, bytes.Length)) != 0)
while((read = reader.Read(bytes, 0, bytes.Length)) != 0) 
 61
 {
{
 62 DataStream.Write(bytes, 0, read);
DataStream.Write(bytes, 0, read);
 63 }
}
 64 reader.Close();
reader.Close(); 
 65 DataStream.Flush();
DataStream.Flush();
 66 DataStream.Close();
DataStream.Close();
 67 }
}
 68 }
}
 69 catch(SocketException ex)
catch(SocketException ex)
 70
 {
{ 
 71 MessageBox.Show(ex.ToString());
MessageBox.Show(ex.ToString());
 72 }
}
 73 }
}
 74
 75 public void DownloadToClient(String server, string remotefilename, string localfilename)
public void DownloadToClient(String server, string remotefilename, string localfilename) 
 76
 {
{
 77 try
try
 78
 {
{
 79 TcpClient tcpc = new TcpClient();
TcpClient tcpc = new TcpClient(); 
 80 Byte[] read = new Byte[1024];
Byte[] read = new Byte[1024]; 
 81
 82 OptionsLoader ol = new OptionsLoader();
OptionsLoader ol = new OptionsLoader(); 
 83 int port = 0;
int port = 0;
 84 if (ol.Port  0)
if (ol.Port  0)
 85
 {
{
 86 port = ol.Port;
port = ol.Port;
 87 }
}
 88 else
else
 89
 {
{
 90 // 缺省的端口号,可以设置为使用的端口号
// 缺省的端口号,可以设置为使用的端口号
 91 port = 8081;
port = 8081;
 92 }
} 
 93
 94
 95 // 尝试与服务器连接
// 尝试与服务器连接
 96 IPHostEntry IPHost = Dns.Resolve(server);
IPHostEntry IPHost = Dns.Resolve(server); 
 97 string []aliases = IPHost.Aliases;
string []aliases = IPHost.Aliases; 
 98 IPAddress[] addr = IPHost.AddressList;
IPAddress[] addr = IPHost.AddressList; 
 99
100 IPEndPoint ep = new IPEndPoint(addr[0], port);
IPEndPoint ep = new IPEndPoint(addr[0], port);
101 tcpc.Connect(ep);
tcpc.Connect(ep);
102
103 // 获得流对象
// 获得流对象
104 Stream s = tcpc.GetStream();
Stream s = tcpc.GetStream(); 
105 Byte[] b = Encoding.ASCII.GetBytes(remotefilename.ToCharArray());
Byte[] b = Encoding.ASCII.GetBytes(remotefilename.ToCharArray());
106 s.Write( b, 0, b.Length );
s.Write( b, 0, b.Length );
107 int bytes;
int bytes;
108 FileStream fs = new FileStream(localfilename, FileMode.OpenOrCreate);
FileStream fs = new FileStream(localfilename, FileMode.OpenOrCreate);
109 BinaryWriter w = new BinaryWriter(fs);
BinaryWriter w = new BinaryWriter(fs);
110
111 // 读取流对象,并将其转换为ASCII码
// 读取流对象,并将其转换为ASCII码
112 while( (bytes = s.Read(read, 0, read.Length)) != 0)
while( (bytes = s.Read(read, 0, read.Length)) != 0) 
113
 {
{ 
114 w.Write(read, 0, bytes);
w.Write(read, 0, bytes);
115 read = new Byte[1024];
read = new Byte[1024]; 
116 }
} 
117
118 tcpc.Close();
tcpc.Close();
119 w.Close();
w.Close();
120 fs.Close();
fs.Close();
121 }
}
122 catch(Exception ex)
catch(Exception ex)
123
 {
{
124 throw new Exception(ex.ToString());
throw new Exception(ex.ToString()); 
125 }
}
126 }
} 
127 }
}
128 }
} 
129
来源:http://www.tulaoshi.com/n/20160219/1600880.html
看过《使用C#开发一个简单的P2P应用》的人还看了以下文章 更多>>