Code
1 C # Sliding WindowAlgorithmAchieve UDP traffic control [original] (iii) 2007 - 01 - 15 16 : 27 // Receiver
2
3 Using System;
4 Using System. collections;
5 Using System. net. Sockets;
6 Using System. net;
7 Using System. Threading;
8 Using System. text;
9 Using Seal. Common;
10
11 Namespace Scutnettalkclient
12 {
13 /// <Summary>
14 /// Abstract description of udpreceivewindow.
15 /// </Summary>
16 Public Class Udpreceivewindow
17 {
18 Private Hashtable extends elist = New Hashtable ();
19 Public Udpclient Client = New Udpclient ( 8080 );
20 Public Slidingwindow receivewindow = New Slidingwindow ( 5 );
21
22
23 Public Udpreceivewindow ()
24 {
25 }
26
27 Public Void Receivethread ( Object O)
28 {
29 Ipendpoint remoteip = Null ;
30 While ( True )
31 {
32 Byte [] Buff = Client. Receive ( Ref Remoteip );
33 Object Pack = Formatterhelper. deserialize (buff );
34 If ( Typeof (Msgslice) = Pack. GetType ())
35 {
36 Msgserver MS = (Msgslice) Pack;
37 // Console. writeline ("received data:" + Ms. getsliceindex ());
38 Cancelist. Remove (Ms. getsliceindex ());
39 Cancelist. Add (Ms. getsliceindex (), MS );
40 If ( ! Ms. issimple)
41 {
42 // Normal frame, response information returned
43 Byte [] Actbt = New Byte [ 9 ];
44 Long Index = Ms. getsliceindex ();
45 Byte [] HBT = New Byte [ 1 ];
46 Byte [] IBT = Bitconverter. getbytes (INDEX );
47 HBT [ 0 ] = Udpslidingwindow. msg_slice_complete;
48 Actbt = Msgslice. byteadd (HBT, IBT );
49 // Remoteip. Port = 8211;
50 Client. Send (actbt, actbt. length, remoteip );
51 // Console. writeline ("response to received piece information:" + index );
52 If (Ms. isendslice ())
53 { // Last frame received
54 If (Ms. getsliceindex () + 1 = ( Long ) Cancelist. Count)
55 { // After receiving all, the "receive completed" command is sent.
56 Actbt = New Byte [ 1 ];
57 Actbt [ 0 ] = Udpslidingwindow. msg_complete;
58 Client. Send (actbt, 1 , Remoteip );
59 Byte [] Msgbt = Msgslice. unitemsg (cancelist );
60 Console. writeline ( " All data slices have been received. " );
61 Console. writeline ( " After the received data is reorganized: " + System. Text. asciiencoding. utf8.getstring (msgbt ));
62 Cancelist. Clear ();
63 }
64 Else
65 { // There is a missing package in the middle. traverse the Final Receiving Window, find the missing package, and send the request again. (Optional ?)
66 Console. writeline (Ms. getsliceindex () + 1 ) + " = " + Cancelist. Count + " Data Package missing, waiting for processing ..... " );
67 }
68 }
69 }
70 }
71 // Thread. Sleep (50 );
72 }
73 }
74 /*
75 [Stathread]
76 Static void main (string [] ARGs)
77 {
78 Udpreceivewindow urw = new udpreceivewindow ();
79 System. Threading. threadpool. queueuserworkitem (New waitcallback (urw. receivethread ));
80 Console. Readline ();
81 }
82 // */
83 }
84 }
85
86
87 Postscript:
88
89 We recommend that UDP data be transmitted between different CIDR blocks in about 1024 bytes. When the maximum transmission unit of the route is exceeded, the route should be split and reorganized to affect the transmission speed. BenProgramWhen the packet size is 1500 bytes in a single packet, it cannot be transmitted over the Internet, probably because of the relationship between local routes. You can modify the corresponding parameters in the LAN. During the test, the size of a single package is changed to 64 KB, And the retransmission delay is changed to half a second. The speed can reach 1.5 MB. / Seconds. In addition, the serialization and deserialization functions used in this example are encapsulated in DLL. You can compile it on your own.
90
91
92