请大侠帮我看看这个代码,关于以阻塞式Socket发送数据(100分)

  • 主题发起人 主题发起人 Mortimer
  • 开始时间 开始时间
M

Mortimer

Unregistered / Unconfirmed
GUEST, unregistred user!
我是一名初学者,想以阻塞式socket发送数据(比如一个zip文件)远端机,下面是我的源程序,
可是运行时产生蓝屏错误。请高手指出这段程序中的错误,或可以改善的地方,我的最
终目的是想实现阻塞式发送数据的功能。先谢了。


unit Unit_Main;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp,StdCtrls;

type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
ServerSocket1: TServerSocket;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
Edit2: TEdit;
Button3: TButton;
procedure Button2Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1GetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
private
{ Private declarations }
public
{ Public declarations }
end;

type
TThreadCli = class(TThread)
FFilename: string;
FDestIP: string;
private

protected
procedure Execute; override;
public
constructor Create(Suspended:Boolean;xc_FileName,xc_DestIP:string);
end;

type
TThreadSvr = class(TServerClientThread)
private

protected
procedure ClientExecute; override;
public

end;

var
Form1: TForm1;

implementation


{$R *.DFM}

{ TThreadCli }


constructor TThreadCli.Create(Suspended: Boolean; xc_FileName,
xc_DestIP: string);
begin
FFileName := xc_FileName;
FDestIP := xc_DestIP;
FreeOnTerminate := true;
inherited create(false);
end;

procedure TThreadCli.Execute;
var
wss: TWinSocketStream;
fs : TFileStream;
buf: PByteArray;
writelen,writenlen: integer;
begin
writenlen := 0; //初始化发送数据计数
fs := TFileStream.Create(FFileName,fmShareDenyWrite);
wss := TWinSocketStream.Create(Form1.ClientSocket1.Socket,3000);
fs.WriteBuffer(buf^,fs.size);
try
repeat
if not terminated then
begin
writelen := wss.Write(buf,1024);
writenlen := writenlen + writelen;
end;
until (writenlen=0);
finally
wss.Free;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ClientSocket1.Address := edit1.Text;
ClientSocket1.Open;
end;

procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
var
ObjThreadCli1 : TThreadCli;
begin
ObjThreadCli1 := TThreadCli.Create(false,Form1.edit2.text,
form1.clientsocket1.address);
end;

{ TThreadSvr }

procedure TThreadSvr.ClientExecute;
var
wss: TWinSocketStream;
buf: PByteArray;
readlen,readedlen,savelen: integer;
fs: TFileStream;
ok: boolean;
begin
readedlen := 0;
ok := false;
inherited FreeOnTerminate := TRUE;
wss := TWinSocketStream.Create(clientsocket,100000);
try
while ( not Terminated ) and ( ClientSocket.Connected ) do
begin
if wss.WaitForData(30000) then
begin
readlen := wss.Read(buf,1024);
readedlen := readedlen + readlen;
if readlen=0 then
begin
ok := true;
break;
end;
end;
end;
if ok=true then
begin
try
if form1.savedialog1.execute then
fs := TFileStream.Create(form1.savedialog1.filename,fmOpenWrite);
repeat
savelen := fs.write(buf,1024);
until (savelen=0);
finally
fs.Free;
end;
end;
finally
wss.Free;
end;
end;

procedure TForm1.ServerSocket1GetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
var
ObjThreadSvr: TThreadSvr;
begin
ObjThreadSvr := TThreadSvr.Create(false,ClientSocket);
end;

end.
 
调试时在什么地方出现蓝屏???
加些注释!!!
 
你的TThreadCli.Execute和TThreadSvr.ClientExecute两个函数中的buf是指针类型未分配空间
直接使用会访问非法内存单元引起蓝屏,而且要进行改动

procedure TThreadCli.Execute;

writelen := wss.Write(buf,1024); -> writelen := wss.Write(buf^,1024);

procedure TThreadSvr.ClientExecute;

readlen := wss.Read(buf,1024); -> readlen := wss.Read(buf^,1024);
savelen := fs.write(buf,1024); -> savelen := fs.write(buf^,1024);

 
后退
顶部