⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unitjs.~pas

📁 用Delphi开发语言实现的对网络链路层的连续ARQ协议的仿真。
💻 ~PAS
字号:
unit Unitjs;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, Sockets;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Label1: TLabel;
    Memo2: TMemo;
    Label2: TLabel;
    CheckBox1: TCheckBox;
    StatusBar1: TStatusBar;
    Button1: TButton;
    TcpServer1: TTcpServer;
    TcpClient1: TTcpClient;
    procedure TcpServer1Accept(Sender: TObject;
      ClientSocket: TCustomIpClient);
    procedure FormCreate(Sender: TObject);
    procedure sendack;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  InfoList:TStringlist;
  info,acks: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.TcpServer1Accept(Sender: TObject;
  ClientSocket: TCustomIpClient);
  var
    t0,t1:integer;
    tou,wei:string;
begin
    tou:=ClientSocket.Receiveln;
    wei:=tou;
    delete(tou,10,length(tou)-9);
    delete(wei,1,9);
    t0:=Strtoint(tou);
    t1:=t0 div 1000;
    t1:=checksum(t1);
    if strtoint(wei)= info then begin
      if t0=t1 then begin
      InfoList.Add(inttostr(t0 div 1000));
      memo1.Lines.Add('第'+wei+'帧'+InfoList.Strings[info]);
      info:=info+1;
      sendack;
      acks:=acks+1;  //正确接收才将ack加1
      end
      else begin     //校验出错
      if  InfoList.Strings[info]='1111111111111' then
      memo1.Lines.Add('第'+wei+'帧'+InfoList.Strings[info]+'校验出错')
      else
      memo1.Lines.Add('第'+wei+'帧丢失');
      sendack;
      end;
    end
    else begin        //序号出错
    if InfoList.Strings[info]='2222222222222'then
    memo1.Lines.Add('第'+wei+'帧丢失')
    else
    memo1.Lines.Add('帧序号出错');
    sendack;
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  InfoList:=TStringlist.Create;
  TcpServer1.Active:=true;
  info:=0;
  acks:=0;
end;

procedure TForm1.sendack;
begin
  TcpClient1.Connect;
  TcpClient1.Sendln('ack'+inttostr(acks));
  TcpClient1.Disconnect;  
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -