一段很复杂的循环,我搞不定了。那位可以帮帮忙???(我的全部家当9分) (9分)

  • 主题发起人 主题发起人 carem1
  • 开始时间 开始时间
C

carem1

Unregistered / Unconfirmed
GUEST, unregistred user!
这是一个局域网内的扫描程序,用nbtstat的功能发送数据包。通过分析返回的数据包获取工作组,用户名,ip等等讯息。单个获取我已经实现了,但是现在要循环192.168.0.0-192.168.0.255这个段的时候。出现了逻辑问题。大家帮我看看。先谢谢了~!!!!!

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,winsock,StdCtrls, ComCtrls, Menus, ExtCtrls;

const
WM_SOCK = WM_USER + 1; //自定义windows消息
UDPPORT = 6767; //设定UDP端口号
NBTPORT = 137;

type
TForm1 = class(TForm)
ListView1: TListView;
StatusBar1: TStatusBar;
PopupMenu1: TPopupMenu;
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
N8: TMenuItem;
Help1: TMenuItem;
N9: TMenuItem;
N10: TMenuItem;
N11: TMenuItem;
N12: TMenuItem;
Panel1: TPanel;
N13: TMenuItem;
N14: TMenuItem;
N15: TMenuItem;
N16: TMenuItem;
N17: TMenuItem;
N18: TMenuItem;
N19: TMenuItem;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure N7Click(Sender: TObject);
procedure N5Click(Sender: TObject);
procedure ListView1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
s: TSocket;
addr: TSockAddr;
FSockAddrIn : TSockAddrIn; //利用消息实时获知UDP消息
procedure ReadData(var Message: TMessage); message WM_SOCK;
public
{ Public declarations }
procedure SendData(b:array of byte);

procedure GetInfo(buffer: Array of byte;len:integer);
end;
var
Form1: TForm1;
nowip:string;

implementation

{$R *.dfm}

procedure tform1.GetInfo(buffer: Array of byte;len:integer);
var
i,j,pos,name_num: integer;
str,group,name,computer,mac:string;
begin
name_num:=0;
for i:=1 to len do
begin
if((buffer=$21)and(buffer[i+1]=$00)and(buffer[i+2]=$01))
then
begin
name_num:=buffer[i+9];
break;
end;
end;
if name_num=0 then exit;
pos:=i+10;
str:='';
for i:=pos to (pos+18*name_num-1) do
begin
if (((i-pos)mod 18) =0) then
begin
for j:=0 to 14 do
begin
if trim(char(buffer[i+j]))='' then buffer[i+j]:=ord(' ');
str:=str+char(buffer[i+j]);
end;
if (buffer[i+16] and $80)=$80 then
begin
if buffer[i+15]=$0 then group:=trim(str)+'<工作组>';
str:=str+format('<%x>',[buffer[i+15]]);
str:=str+'<GROUP>';
end
else
begin

if buffer[i+15]=$20 then computer:=str
else if buffer[i+15]=$3 then name:=str;
str:=str+format('<%x>',[buffer[i+15]]);
str:=str+'<UNIQUE>';

end;
str:='';
end;
end;

for i:=0 to 5 do
begin
str:=str+format('%.2x.',[buffer[i+pos+18*name_num]]);
end;
delete(str,length(str),1);
mac:=trim(str);
listview1.Items.Add.Caption:=computer;
listview1.Items[0].subitems.add(nowip);
listview1.Items[0].SubItems.add(mac);
listview1.Items[0].SubItems.add(name);
end;

procedure Tform1.ReadData(var Message: TMessage);
var
buffer: Array [1..500] of byte;
len{,i}: integer;
flen: integer;
Event: word;
value: string;

begin
value:='';
flen:=sizeof(FSockAddrIn);
FSockAddrIn.SIn_Port := htons(NBTPORT);
Event := WSAGetSelectEvent(Message.LParam);
if Event = FD_READ then
begin
len := recvfrom(s, buffer, sizeof(buffer), 0, FSockAddrIn, flen);
{for i:=1 to len do value:=value+format('%x',[buffer]);
ListBox1.items.add(value);
value:='';
for i:=1 to len do if char(buffer)<>#0 then value:=value+char(buffer);
ListBox1.items.add(value);}
if len<> 0 then GetInfo(buffer,len);
end;
end;

procedure tform1.SendData(b:array of byte);
var
tempip,len: integer;
begin
for tempip:=0 to 255 do //////这里循环对么??我的头都大啦~!!
begin
nowip := '192.168.0.'+inttostr(tempip);
StatusBar1.SimpleText:=nowip;
FSockAddrIn.SIn_Addr.S_addr := inet_addr(pchar(nowip));
len := sendto(s, b[0],50, 0, FSockAddrIn, sizeof(FSockAddrIn));
//if (WSAGetLastError() <> WSAEWOULDBLOCK) and (WSAGetLastError() <> 0) then showmessage(inttostr(WSAGetLastError()));
if len = SOCKET_ERROR then
showmessage('send fail');
if len <> 50 then
showmessage('Not Send all');
end
end;

procedure TForm1.FormCreate(Sender: TObject);
var
TempWSAData: TWSAData;
//optval: integer;
begin
// 初始化SOCKET
if WSAStartup($101, TempWSAData)=1 then
showmessage('StartUp Error!');
s := Socket(AF_INET, SOCK_DGRAM, 0);
if (s = INVALID_SOCKET) then //Socket创建失败
begin
showmessage(inttostr(WSAGetLastError())+' Socket创建失败');
CloseSocket(s);
end;
//本机SockAddr绑定
addr.sin_family := AF_INET;
addr.sin_addr.S_addr := INADDR_ANY;
addr.sin_port := htons(UDPPORT);
if Bind(s, addr, sizeof(addr)) <> 0 then
begin
showmessage('bind fail');
end;
WSAAsyncSelect(s, form1.Handle , WM_SOCK, FD_READ);
//对方SockAddrIn设定
FSockAddrIn.SIn_Family := AF_INET;
FSockAddrIn.SIn_Port := htons(NBTPORT);
end;

procedure TForm1.N7Click(Sender: TObject);
const NbtstatPacket:array[0..49]of byte
=($0,$0,$0,$10,$0,$1,
$0,$0,$0,$0,$0,$0,$20,$43,$4b,
$41,$41,$41,$41,$41,$41,$41,$41,
$41,$41,$41,$41,$41,$41,$41,$41,
$41,$41,$41,$41,$41,$41,$41,$41,
$41,$41,$41,$41,$41,$41,$0,$0,$21,$0,$1);
begin
senddata(NbtstatPacket);
end;

end.
 
192.168.0.0 是非法ip

从192.168.0.1开始 到192.168.0.254结束 255是广播地址

 
现在不是ip地址的问题。是运行之后的结果问题!~!··
 
你把dfm文件也贴上来吧
 
dfm文件

object Form1: TForm1
Left = 242
Top = 256
Width = 698
Height = 458
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
Menu = MainMenu1
OldCreateOrder = False
OnClose = FormClose
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object StatusBar1: TStatusBar
Left = 0
Top = 393
Width = 690
Height = 19
Panels = <>
SimplePanel = True
end
object Panel1: TPanel
Left = 0
Top = 0
Width = 689
Height = 393
Caption = 'Panel1'
TabOrder = 1
object ListView1: TListView
Left = 1
Top = 2
Width = 687
Height = 392
Columns = <
item
Caption = #35745#31639#26426
end
item
Caption = 'IP'#22320#22336
end
item
Caption = 'MAC'#22320#22336
end
item
Caption = #24403#21069#29992#25143
end
item
Caption = #36828#31243#25511#21046
end>
MultiSelect = True
ReadOnly = True
PopupMenu = PopupMenu1
TabOrder = 0
ViewStyle = vsReport
OnMouseDown = ListView1MouseDown
end
end
object PopupMenu1: TPopupMenu
Left = 496
Top = 8
object N13: TMenuItem
Caption = #36830#25509
end
object N14: TMenuItem
Caption = #26029#24320
end
object N15: TMenuItem
Caption = #23631#24149
end
object N16: TMenuItem
Caption = #20851#26426
end
object N17: TMenuItem
Caption = #37325#21551
end
object N18: TMenuItem
Caption = #38145#23450
end
object N19: TMenuItem
Caption = #25991#20214
end
end
object MainMenu1: TMainMenu
Left = 528
Top = 8
object N1: TMenuItem
Caption = #25991#20214
object N2: TMenuItem
Caption = #36830#25509
end
object N3: TMenuItem
Caption = #26029#24320
end
object N4: TMenuItem
Caption = '-'
end
object N5: TMenuItem
Caption = #36864#20986
OnClick = N5Click
end
end
object N6: TMenuItem
Caption = #25805#20316
object N7: TMenuItem
Caption = #21015#20030
OnClick = N7Click
end
object N8: TMenuItem
Caption = #32456#27490
end
end
object Help1: TMenuItem
Caption = 'Help'
object N9: TMenuItem
Caption = #24110' '#21161
end
object N10: TMenuItem
Caption = #22312#32447#21319#32423
end
object N11: TMenuItem
Caption = '-'
end
object N12: TMenuItem
Caption = #20851#20110' [About]'
end
end
end
end
 
可能是数据发送过快了
for tempip := 1 to 254 do //////这里循环对么??我的头都大啦~!!
begin
Sleep(50);
nowip := '192.168.0.' + inttostr(tempip);
StatusBar1.SimpleText := nowip;
FSockAddrIn.SIn_Addr.S_addr := inet_addr(pchar(nowip));
len := sendto(s, b[0], 50, 0, FSockAddrIn, sizeof(FSockAddrIn));
//if (WSAGetLastError() <> WSAEWOULDBLOCK) and (WSAGetLastError() <> 0) then showmessage(inttostr(WSAGetLastError()));
if len = SOCKET_ERROR then
showmessage('send fail');
if len <> 50 then
showmessage('Not Send all');
end;

停顿一下就没事了。
 
to:andy263
是好了一点,但是结果增加的地方不对
 
救民嘎~~~~!!!
 
接受答案了.
 
后退
顶部