📄 zzunit.~pas
字号:
unit zzUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, ExtCtrls, Sockets, Spin;
type
TForm1 = class(TForm)
StatusBar1: TStatusBar;
Memo2: TMemo;
LabeledEdit1: TLabeledEdit;
LabeledEdit2: TLabeledEdit;
Button1: TButton;
TcpServer1: TTcpServer;
TcpClient1: TTcpClient;
TcpClient2: TTcpClient;
TcpServer2: TTcpServer;
Timer1: TTimer;
Timer2: TTimer;
ListBox1: TListBox;
SpinEdit1: TSpinEdit;
Label1: TLabel;
procedure TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
procedure TcpServer2Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure sendinfo;
procedure sendack;
procedure Timer1Timer(Sender: TObject);
procedure Timer2Timer(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
InfoList,AckList:TStringList;
info,acks:integer;
showinfo,showack,infoerror,infolose:integer;
implementation
{$R *.dfm}
procedure TForm1.sendinfo;
var
temp:string;
begin
temp:=InfoList.Strings[info];
randomize;
if (random(infoerror-1)=0)and(infoerror>0) then
temp:='1111111111111'; //这个代表错误帧
TcpClient2.Connect;
TcpClient2.Sendln(temp);
TcpClient2.Disconnect;
ListBox1.Selected[info]:=true;
info:=info+1;
end;
procedure TForm1.sendack;
begin
TcpClient1.Connect;
TcpClient1.Sendln(AckList.Strings[acks]);
TcpClient1.Disconnect;
memo2.Lines.Append('已发送'+AckList.Strings[acks]);
acks:=acks+1;
end;
procedure TForm1.TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient); {收到信息帧}
begin
if Timer1.Enabled=false then Timer1.Enabled:=true;
InfoList.Add(ClientSocket.Receiveln);
ListBox1.Items.Add(InfoList.Strings[showinfo]);
showinfo:=showinfo+1;
end;
procedure TForm1.TcpServer2Accept(Sender: TObject;
ClientSocket: TCustomIpClient); {收到监督帧}
begin
if Timer2.Enabled=false then Timer2.Enabled:=true;
AckList.Add(ClientSocket.Receiveln);
memo2.Lines.Append('已收到'+AckList.Strings[showack]);
showack:=showack+1;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
InfoList:=TStringList.Create;
AckList:=TStringList.Create;
info:=0;
acks:=0;
showinfo:=0;
showack:=0;
TcpServer2.Active:=true;
TcpServer1.Active:=true;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
InfoList.Free;
AckList.Free;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin //发送信息帧
if info<InfoList.Count then
sendinfo;
end;
procedure TForm1.Timer2Timer(Sender: TObject);
begin //发送监督帧
if acks<AckList.Count then
sendack;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
timer1.Interval:=spinedit1.Value*1000;
timer2.Interval:=spinedit1.Value*1000;
if LabeledEdit2.Text<>'0'then
infoerror:=100 div strtoint(LabeledEdit2.Text)
else infoerror:=0;
if LabeledEdit1.Text<>'0'then
infolose:=100 div strtoint(LabeledEdit1.Text)
else infolose:=0;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -