test.pas
上传用户:hbtcygglw
上传日期:2007-01-07
资源大小:281k
文件大小:4k
源码类别:

其他

开发平台:

Delphi

  1. unit test;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  5.   StdCtrls, Db, DBTables, Grids, DBGrids,data,winsock;
  6. type
  7.   TForm1 = class(TForm)
  8.     Button1: TButton;
  9.     Label1: TLabel;
  10.     AddrTable: TTable;
  11.     DataSource1: TDataSource;
  12.     AddrTableSTARTIP: TStringField;
  13.     AddrTableENDIP: TStringField;
  14.     AddrTableCOUNTRY: TStringField;
  15.     AddrTableLOCAL: TStringField;
  16.     AddrTableTHANK: TStringField;
  17.     Button2: TButton;
  18.     Edit1: TEdit;
  19.     Button3: TButton;
  20.     ListBox: TListBox;
  21.     Button4: TButton;
  22.     Button5: TButton;
  23.     procedure Button1Click(Sender: TObject);
  24.     procedure Button2Click(Sender: TObject);
  25.     procedure Button3Click(Sender: TObject);
  26.     procedure FormCreate(Sender: TObject);
  27.     procedure Button4Click(Sender: TObject);
  28.     procedure Button5Click(Sender: TObject);
  29.   private
  30.     { Private declarations }
  31.     FSocket:TSocket;
  32.     Loop:Boolean;
  33.   public
  34.     { Public declarations }
  35.   end;
  36. //const
  37. //SOCK_RAW=3;
  38. //IPPROTO_RAW=255;
  39. var
  40.   Form1: TForm1;
  41. implementation
  42. {$R *.DFM}
  43. uses info;
  44. function StrToIp(str:string):DWORD;
  45. var
  46. s:string;
  47. l,i,parse:Integer;
  48. adr:array [0..3] of DWORD;
  49. begin
  50. parse:=0;
  51. l:=length(str);
  52. for i:=1 to l+1 do
  53.     begin
  54.     if(i<=l) and (str[i]<>'.')then
  55.         begin
  56.         s:=s+str[i];
  57.         end
  58.     else
  59.         begin
  60.         adr[parse]:=StrToIntDef(s,0);
  61.         Inc(parse);
  62.         s:='';
  63.         end;
  64.     end;
  65. Result:=(adr[0]shl 24)or(adr[1] shl 16) or(adr[2] shl 8) or adr[3];
  66. end;
  67. function IPEqu(ip1,ip2:string):Boolean;
  68. begin
  69. Result:=(StrToIp(ip1)=StrToIp(ip2));
  70. end;
  71. procedure TForm1.Button1Click(Sender: TObject);
  72. var
  73. f:File of TIPInfo;
  74. info:TIPInfo;
  75. begin
  76. AssignFile(f,'IP.dat');
  77. Rewrite(f);
  78. AddrTable.Open;
  79. while not AddrTable.Eof do
  80.     begin
  81.     info.StartIp:=AddrTable.FieldByName('StartIp').AsString;
  82.     info.EndIp:=AddrTable.FIeldBYName('EndIp').AsString;
  83.     info.Country:=AddrTable.FieldByName('Country').AsString;
  84.     info.Location:=AddrTable.FieldByName('LOCAL').AsString;
  85.     Write(f,info);
  86.     AddrTable.Next;
  87.     end;
  88. AddrTable.Close;
  89. CloseFile(f);
  90. end;
  91. procedure TForm1.Button2Click(Sender: TObject);
  92. begin
  93. LoadIPInfo('ip.dat');
  94. ShowMessage(IntToStr(IpInfoCnt));
  95. end;
  96. procedure TForm1.Button3Click(Sender: TObject);
  97. var
  98. IpInf:TIPInfo;
  99. begin
  100. IpInf:=FindIPInfo(Edit1.Text);
  101. Edit1.Text:=IpInf.Country+IpInf.Location;
  102. end;
  103. procedure TForm1.FormCreate(Sender: TObject);
  104. begin
  105. LoadIPInfo('ip.dat');
  106. end;
  107. procedure TForm1.Button4Click(Sender: TObject);
  108. var
  109. d:TWSAData;
  110. val:Boolean;
  111. addr:TSockAddr;
  112. sz:Integer;
  113. buf:array [0..2048] of char;
  114. begin
  115. WSAStartup(2,d);
  116. FSocket:=socket(AF_INET,SOCK_RAW,IPPROTO_UDP);
  117. if(FSocket=INVALID_SOCKET)then
  118.     begin
  119.     ShowMessage('socket error:'+IntToStr(GetLastError));
  120.     end;
  121. val:=True;
  122. {if(setsockopt(FSocket,SOl_SOCKET,SO_REUSEADDR,@val,sizeof(val))=SOCKET_ERROR)then
  123.     begin
  124.     ShowMessage('setsockopt error:'+IntToStr(GetLastError));
  125.     end;}
  126. addr.sin_family:=AF_INET;
  127. addr.sin_port:=4000;
  128. addr.sin_addr.s_addr := INADDR_ANY;
  129. if(bind(FSocket,addr,sizeof(addr))=SOCKET_ERROR)then
  130.     begin
  131.     ShowMessage('setsockopt error:'+IntToStr(GetLastError));
  132.     end;
  133. sz:=sizeof(addr);
  134. button4.enabled:=false;
  135. button5.enabled:=true;
  136. Loop:=True;
  137. {while(Loop)do
  138.     begin
  139.     recvfrom(FSocket,buf,2048,0,addr,sz);
  140.     ListBox.Items.add(buf);
  141.     Application.ProcessMessages;
  142.     end;}
  143. end;
  144. procedure TForm1.Button5Click(Sender: TObject);
  145. begin
  146. Loop:=False;
  147. closesocket(FSOCKET);
  148. WSACleanup;
  149. button4.enabled:=true;
  150. button5.enabled:=false;
  151. end;
  152. end.