(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)
C#语言由于其对网络功能良好的支持,特别是内置地支持TCPListener和TCPClient这二个类,使得利用它开发P2P应用程序变得非常容易。下面就是一个使用C#开发的P2P应用的例子:
1public MyTcpListener(int port) : base(port)
2
3public void StopMe()
4{
5if ( this.Server != null )
6
7}
8}
9
10public class Transfer
11{
12MyTcpListener tcpl;
13
14public Transfer()
15{
16OptionsLoader ol = new OptionsLoader();
17int port = 8081;
18if (ol.Port 0)
19{
20port = ol.Port;
21}
22else
23{
24port = 8081;
25}
26
27this.tcpl = new MyTcpListener(port);
28}
29
30public void TransferShutdown()
31
32
33public void ListenForPeers()
34{
35try
36{
37
38Encoding ASCII = Encoding.ASCII;
39
40
41tcpl.Start();
42
43
44while (true)
45{
46// 在有连接之前,Accept将处于阻塞状态
47Socket s = tcpl.AcceptSocket();
48NetworkStream DataStream = new NetworkStream(s);
49
50String filename;
51Byte[] Buffer = new Byte[256];
52DataStream.Read(Buffer, 0, 256);
53filename = Encoding.ASCII.GetString(Buffer);
54StringBuilder sbFileName = new StringBuilder(filename);
55StringBuilder sbFileName2 = sbFileName.Replace("", "");
56FileStream fs = new FileStream(sbFileName2.ToString(), FileMode.Open, FileAccess.Read);
57BinaryReader reader = new BinaryReader(fs);
58byte[] bytes = new byte[1024];
59int read;
60while((read = reader.Read(bytes, 0, bytes.Length)) != 0)
61{
62DataStream.Write(bytes, 0, read);
63}
64reader.Close();
65DataStream.Flush();
66DataStream.Close();
67}
68}
69catch(SocketException ex)
70{
71MessageBox.Show(ex.ToString());
72}
73}
74
75public void DownloadToClient(String server, string remotefilename, string localfilename)
76{
77try
78{
79TcpClient tcpc = new TcpClient();
80Byte[] read = new Byte[1024];
81
82OptionsLoader ol = new OptionsLoader();
83int port = 0;
84if (ol.Port 0)
85{
86port = ol.Port;
87}
88else
89{
90// 缺省的端口号,可以设置为使用的端口号
91port = 8081;
92}
93
94
95// 尝试与服务器连接
96IPHostEntry IPHost = Dns.Resolve(server);
97string []aliases = IPHost.Aliases;
98IPAddress[] addr = IPHost.AddressList;
99
100IPEndPoint ep = new IPEndPoint(addr[0], port);
101tcpc.Connect(ep);
102
103// 获得流对象
104Stream s = tcpc.GetStream();
105Byte[] b = Encoding.ASCII.GetBytes(remotefilename.ToCharArray());
106s.Write( b, 0, b.Length );
107int bytes;
108FileStream fs = new FileStream(localfilename, FileMode.OpenOrCreate);
109BinaryWriter w = new BinaryWriter(fs);
110
111// 读取流对象,并将其转换为ASCII码
112while( (bytes = s.Read(read, 0, read.Length)) != 0)
113{
114w.Write(read, 0, bytes);
115read = new Byte[1024];
116}
117
118tcpc.Close();
119w.Close();
120fs.Close();
121}
122catch(Exception ex)
123{
124throw new Exception(ex.ToString());
125}
126}
127}
128}
129