用Delphi开发语言实现的对网络链路层的连续ARQ协议的仿真。

源代码在线查看: sender.~pas

软件大小: 572 K
上传用户: apsdn
关键词: Delphi ARQ 语言 网络链路
下载地址: 免注册下载 普通下载 VIP

相关代码

				unit sender;
				
				interface
				
				uses
				  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
				  Dialogs, StdCtrls, Sockets, ComCtrls, ExtCtrls, Spin;
				
				type
				  TForm1 = class(TForm)
				    ListBox1: TListBox;
				    TcpClient1: TTcpClient;
				    TcpServer1: TTcpServer;
				    Button1: TButton;
				    Button2: TButton;
				    Memo1: TMemo;
				    StatusBar1: TStatusBar;
				    Button3: TButton;
				    Timer0: TTimer;
				    SpinEdit1: TSpinEdit;
				    Label1: TLabel;
				    Timer1: TTimer;
				    Timer2: TTimer;
				    Timer3: TTimer;
				    Timer4: TTimer;
				    Timer5: TTimer;
				    Timer6: TTimer;
				    OpenDialog1: TOpenDialog;
				    procedure Button2Click(Sender: TObject);
				    procedure Sendframe;
				    procedure Button1Click(Sender: TObject);
				    procedure FormCreate(Sender: TObject);
				    procedure Timer0Timer(Sender: TObject);
				    procedure Button3Click(Sender: TObject);
				    procedure TcpServer1Accept(Sender: TObject;
				      ClientSocket: TCustomIpClient);
				    procedure Timer1Timer(Sender: TObject);
				    procedure Timer2Timer(Sender: TObject);
				    procedure Timer3Timer(Sender: TObject);
				    procedure Timer4Timer(Sender: TObject);
				    procedure Timer5Timer(Sender: TObject);
				    procedure Timer6Timer(Sender: TObject);
				  private
				    { Private declarations }
				  public
				    { Public declarations }
				  end;
				
				var
				  Form1: TForm1;
				  sends,gets,k:integer;
				  send:boolean;
				  t1,t2,t3,t4,t5,t6:integer;
				implementation
				
				{$R *.dfm}
				
				function checksum(x:integer):integer;
				var
				  t,y:integer;
				begin
				  t:= x div 999;
				  y:= x-t*999;
				  x:=x*1000;
				  result:=x+y;
				end;
				
				procedure TForm1.Sendframe;
				var
				sor:integer;
				info:string;
				begin
				  if sends>0 then
				    ListBox1.Selected[sends-1]:=false;
				    sor:=strtoint(Listbox1.Items.Strings[sends]);
				    info:=inttostr(checksum(sor))+inttostr(sends);
				    TcpClient1.Connect;
				    TcpClient1.Sendln(info);
				    TcpClient1.Disconnect;
				    ListBox1.Selected[sends]:=true;
				    SpinEdit2.Value:=sends;
				    sends:=sends+1;
				end;
				
				procedure TForm1.Button2Click(Sender: TObject);
				begin
				   Timer0.Enabled:=true;
				   Button2.Enabled:=false;
				end;
				
				procedure TForm1.Button1Click(Sender: TObject);
				var
				  jiange:integer;
				begin
				if   Button1.Caption='启动' then begin
				     TcpClient1.Active:=true;
				     Timer0.Interval:= SpinEdit1.Value *1000;
				     TcpServer1.Active:=true;
				     Button1.Caption:='暂停发送';
				     jiange:=20;
				     timer1.Interval:=jiange*1000;
				     timer2.Interval:=jiange*1000;
				     timer3.Interval:=jiange*1000;
				     timer4.Interval:=jiange*1000;
				     timer5.Interval:=jiange*1000;
				     timer6.Interval:=jiange*1000;
				     if sends>0 then Timer0.Enabled:=true;
				     end
				     else begin
				     Timer0.Enabled:=false;
				     timer1.Enabled:=false;
				     timer2.Enabled:=false;
				     timer3.Enabled:=false;
				     timer4.Enabled:=false;
				     timer5.Enabled:=false;
				     timer6.Enabled:=false;
				     TcpClient1.Active:=false;
				     Button1.Caption:='启动'
				     end;
				end;
				
				procedure TForm1.FormCreate(Sender: TObject);
				begin
				      sends:=0;
				      gets:=0;
				end;
				
				procedure TForm1.Timer0Timer(Sender: TObject);
				begin
				  if sends-gets				  case (sends mod 6) of
				  0:begin
				  t6:=sends;
				  Timer6.Enabled:=true;
				  end;
				  1:begin
				  t1:=sends;
				  Timer1.Enabled:=true;
				  end;
				  2:begin
				  t2:=sends;
				  Timer2.Enabled:=true;
				  end;
				  3:begin
				  t3:=sends;
				  Timer3.Enabled:=true;
				  end;
				  4:begin
				  t4:=sends;
				  Timer4.Enabled:=true;
				  end;
				  5:begin
				  t5:=sends;
				  Timer5.Enabled:=true;
				  end;
				end;
				    Sendframe;
				    end
				    else memo1.lines.Append('网络时延过大,发送窗口已满');
				end;
				
				procedure TForm1.Button3Click(Sender: TObject);
				begin    
				close;
				end;
				
				procedure TForm1.TcpServer1Accept(Sender: TObject;
				  ClientSocket: TCustomIpClient);
				var
				   temp:string;
				   m:integer;
				begin
				   temp:= ClientSocket.Receiveln;
				   delete(temp,1,3);
				   m:=strtoint(temp);
				   m:=m mod 6;
				   memo1.Lines.Add('收到ACK'+inttostr(m));
				   if temp=inttostr(gets) then begin
				case (gets mod 6)of
				   0:begin
				   Timer6.Enabled:=false;
				   end;
				   1:begin
				   Timer1.Enabled:=false;
				   end;
				   2:begin
				   Timer2.Enabled:=false;
				   end;
				   3:begin
				   Timer3.Enabled:=false;
				   end;
				   4:begin
				   Timer4.Enabled:=false;
				   end;
				   5:begin
				   Timer5.Enabled:=false;
				   end;
				end;
				   if gets>0 then  ListBox1.Selected[gets-1]:=false;
				   ListBox1.Selected[gets]:=true;
				   gets:=gets+1;
				   end;
				end;
				
				procedure TForm1.Timer1Timer(Sender: TObject);
				begin
				      ListBox1.Selected[sends-1]:=false;
				      memo1.Lines.Append('超时重发第'+inttostr(t1)+'帧');
				      sends:=t1;
				      sendframe;
				end;
				
				procedure TForm1.Timer2Timer(Sender: TObject);
				begin
				      ListBox1.Selected[sends-1]:=false;
				      memo1.Lines.Append('超时重发第'+inttostr(t2)+'帧');
				      sends:=t2;
				      sendframe;
				end;
				
				procedure TForm1.Timer3Timer(Sender: TObject);
				begin
				      ListBox1.Selected[sends-1]:=false;
				      memo1.Lines.Append('超时重发第'+inttostr(t3)+'帧');
				      sends:=t3;
				      sendframe;
				end;
				
				procedure TForm1.Timer4Timer(Sender: TObject);
				begin
				      ListBox1.Selected[sends-1]:=false;
				      memo1.Lines.Append('超时重发第'+inttostr(t4)+'帧');
				      sends:=t4;
				      sendframe;
				end;
				
				procedure TForm1.Timer5Timer(Sender: TObject);
				begin
				      ListBox1.Selected[sends-1]:=false;
				      memo1.Lines.Append('超时重发第'+inttostr(t5)+'帧');
				      sends:=t5;
				      sendframe;
				end;
				
				procedure TForm1.Timer6Timer(Sender: TObject);
				begin
				      ListBox1.Selected[sends-1]:=false;
				      memo1.Lines.Append('超时重发第'+inttostr(t6)+'帧');
				      sends:=t6;
				      sendframe;
				end;
				
				end.
							

相关资源