// 关闭程序...
procedure TForm1.Button4Click(Sender: TObject);
begin
form1.close;
end;
//
// 搜索路由器设备...
procedure TForm1.Button1Click(Sender: TObject);
var
tmpstr: string;
buffer: array [0..4096] of char;
j: integer;
begin
tmpstr:='M-SEARCH * HTTP/1.1'#13#10
+'HOST: 239.255.255.250:1900'#13#10
+'MAN: "ssdp:discover"'#13#10
+'MX: 3'#13#10
+'ST: upnp:rootdevice'#13#10#13#10;
j:=length(tmpstr);
strplcopy(buffer,tmpstr,j);
udp.SendBuffer(buffer,j);
memo1.lines.add('发出了如下命令:'#13#10+tmpstr);
end;
//
// 收到路由器设备信息...
procedure TForm1.UDPDataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
var
tmpstr: string;
buffer: array [0..4096] of char;
j: integer;
begin
memo1.lines.add('从'+fromip+':'+inttostr(port)+'收到'+inttostr(numberbytes)+'个字节的数据!');
if (numberbytes<=0) or (numberbytes>4096) then
exit;
udp.ReadBuffer(buffer,numberbytes);
setlength(tmpstr,numberbytes);
strlcopy(pchar(tmpstr),buffer,numberbytes);
memo1.lines.add('收到的信息如下:'#13#10+tmpstr);
if uppercase(copy(tmpstr,1,5))<>'HTTP/' then
begin
memo1.lines.add('收到的信息不是设备搜寻结果,忽略!');
memo1.lines.add('');
exit;
end;
//
// 解析出Location、Server、USN三个域内容...
location:=tmpstr;
j:=pos('LOCATION:',uppercase(location));
if j<0 then
location:=''
else
begin
delete(location,1,j+8);
j:=pos(#13#10,location);
location:=trim(copy(location,1,j-1));
end;
//
server:=tmpstr;
j:=pos('SERVER:',uppercase(server));
if j<0 then
server:=''
else
begin
delete(server,1,j+6);
j:=pos(#13#10,server);
server:=trim(copy(server,1,j-1));
end;
//
usn:=tmpstr;
j:=pos('USN:',uppercase(usn));
if j<0 then
usn:=''
else
begin
delete(usn,1,j+3);
j:=pos(#13#10,usn);
usn:=trim(copy(usn,1,j-1));
end;
//
// 得到IP地址、端口...
tmpstr:=location;
if copy(uppercase(tmpstr),1,7)='HTTP://' then
delete(tmpstr,1,7);
j:=pos(':',tmpstr);
if j<=0 then
exit;
routerip:=copy(tmpstr,1,j-1);
delete(tmpstr,1,j);
//
j:=pos('/',tmpstr);
if j>1 then