下面的是一个测试程序,如果DHCP Server启动后再点击"active UDP"则会出错;如果先
"active UDP"后再启动DHCP Server,则测试程序会收到DHCP Server发出的包,可是此时
它自己不能给自己发包。这是为什么啊?测试环境 Delphi6.0 + Windows2000
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer,IdSocketHandle;
type
TForm1 = class(TForm)
MyUDP_67: TIdUDPServer;
MyUDP_68: TIdUDPServer;
Memo_67: TMemo;
Memo_68: TMemo;
btn_67Send: TButton;
btn_68Send: TButton;
et_toIP: TEdit;
btn_activeUDP: TButton;
procedure MyUDP_67UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure MyUDP_68UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure FormCreate(Sender: TObject);
procedure btn_67SendClick(Sender: TObject);
procedure btn_68SendClick(Sender: TObject);
procedure btn_activeUDPClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
Inc_67,Inc_68 : Integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.MyUDP_67UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
begin
Memo_67.Lines.Add('我是67,收到一条信息--------'+IntToStr(Inc_67));
Inc(Inc_67);
// btn_67SendClick(Sender);
end;
procedure TForm1.MyUDP_68UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
begin
Memo_68.Lines.Add('我是68,收到一条信息--------'+IntToStr(Inc_68));
Inc(Inc_68);
// btn_68SendClick(Sender);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Inc_67 := 1;
Inc_68 := 1;
end;
procedure TForm1.btn_67SendClick(Sender: TObject);
var strSend : String;
begin
strSend := '21543654645';
MyUDP_67.SendBuffer(et_toIP.Text,68,strSend,SizeOf(strSend));
end;
procedure TForm1.btn_68SendClick(Sender: TObject);
var strSend : String;
begin
strSend := '21543654645';
MyUDP_68.SendBuffer(et_toIP.Text,67,strSend,SizeOf(strSend));
end;
procedure TForm1.btn_activeUDPClick(Sender: TObject);
begin
if btn_activeUDP.Caption='Active UDP' then
begin
try
MyUDP_67.Active:=True;
MyUDP_68.Active:=True;
btn_activeUDP.Caption := 'stop UDP';
except MessageBox(0,PChar('启动失败!'),'',MB_OK);
end;
end
else
begin
MyUDP_67.Active:=False;
MyUDP_68.Active:=False;
btn_activeUDP.Caption := 'Active UDP';
Memo_67.Lines.Add('★★★★★★★★★★★');
Memo_68.Lines.Add('★★★★★★★★★★★');
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
MyUDP_67.Active:=False;
MyUDP_68.Active:=False;
end;
end.