S
solley
Unregistered / Unconfirmed
GUEST, unregistred user!
Server端(在运行这一段时程序退出)
procedure TForm3.ssClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
Df : PDataFlag;
cmd : string;
Buffer : Pointer;
nRetr : Integer;
fs : TFileStream;
Const BufferSize = 1024;
begin
Df := Socket.Data ;
case Df.WorkFlag of
0,3 :
begin
cmd := Trim(Socket.ReceiveText);
if Pos('FILESEND',uppercase(cmd)) > 0 then
begin
Df.FileName := Trim(Copy(cmd,Pos(' ',cmd)+1,Length(cmd)));
Df.FileSize := StrToInt(Copy(Df.FileName,
Pos(' ',Df.FileName)+1,Length(Df.FileName)));
Df.FileName := Trim(Copy(Df.FileName,1,
Pos(' ',Df.FileName)));
Df.WorkFlag := 1;
Socket.Data := Df;
Socket.SendText('文件传送服务准备完毕,可以传送!'#13#10);
RichEdit1.Lines.Add(DateToStr(Date)+' '+
TimeToStr(Time)+'==>>'+'文件名:'+Df.FileName+' 文件大小:'+
IntToStr(Df.FileSize)+#13#10'文件传送服务完毕,可以传送!');
end;
end;
1 :
begin
GetMem(Buffer,BufferSize);
nRetr := Socket.ReceiveBuf(Buffer^,BufferSize);
if not FileExists(Label2.Caption+Df.FileName) then
begin
fs := TFileStream.Create(Label2.Caption+Df.FileName,
fs.seek(0,soFromBeginning));
end
else
begin
fs := TFileStream.Create(Label2.Caption+Df.FileName,
fmOpenWrite or fmShareDenyNone);
fs.Seek(0,soFromEnd);
end;
fs.WriteBuffer(Buffer^,nRetr);
fs.Destroy ;
FreeMem(Buffer);
Df.WorkFlag := 0;
Socket.SendText('文件传送服务准备完毕,可以传送下一个文件!'#13#10)
end;
end;
end;
Client端(发送)
procedure TForm2.N7Click(Sender: TObject);
var
fs : TFileStream;
Buf : Pointer;
begin
fs := TFileStream.Create(Edit1.Text,fmOpenRead or fmShareDenyNone);
GetMem(Buf,fs.size);
fs.Seek(0,soFromBeginning);
fs.ReadBuffer(Buf^,fs.Size);
RichEdit1.Lines.Add(DateToStr(Date)+' '+TimeToStr(Time)+'==>>'+
'共发送:'+IntToStr(cs.Socket.SendBuf(Buf^,fs.Size))+
'字节。' );
end;
procedure TForm3.ssClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
Df : PDataFlag;
cmd : string;
Buffer : Pointer;
nRetr : Integer;
fs : TFileStream;
Const BufferSize = 1024;
begin
Df := Socket.Data ;
case Df.WorkFlag of
0,3 :
begin
cmd := Trim(Socket.ReceiveText);
if Pos('FILESEND',uppercase(cmd)) > 0 then
begin
Df.FileName := Trim(Copy(cmd,Pos(' ',cmd)+1,Length(cmd)));
Df.FileSize := StrToInt(Copy(Df.FileName,
Pos(' ',Df.FileName)+1,Length(Df.FileName)));
Df.FileName := Trim(Copy(Df.FileName,1,
Pos(' ',Df.FileName)));
Df.WorkFlag := 1;
Socket.Data := Df;
Socket.SendText('文件传送服务准备完毕,可以传送!'#13#10);
RichEdit1.Lines.Add(DateToStr(Date)+' '+
TimeToStr(Time)+'==>>'+'文件名:'+Df.FileName+' 文件大小:'+
IntToStr(Df.FileSize)+#13#10'文件传送服务完毕,可以传送!');
end;
end;
1 :
begin
GetMem(Buffer,BufferSize);
nRetr := Socket.ReceiveBuf(Buffer^,BufferSize);
if not FileExists(Label2.Caption+Df.FileName) then
begin
fs := TFileStream.Create(Label2.Caption+Df.FileName,
fs.seek(0,soFromBeginning));
end
else
begin
fs := TFileStream.Create(Label2.Caption+Df.FileName,
fmOpenWrite or fmShareDenyNone);
fs.Seek(0,soFromEnd);
end;
fs.WriteBuffer(Buffer^,nRetr);
fs.Destroy ;
FreeMem(Buffer);
Df.WorkFlag := 0;
Socket.SendText('文件传送服务准备完毕,可以传送下一个文件!'#13#10)
end;
end;
end;
Client端(发送)
procedure TForm2.N7Click(Sender: TObject);
var
fs : TFileStream;
Buf : Pointer;
begin
fs := TFileStream.Create(Edit1.Text,fmOpenRead or fmShareDenyNone);
GetMem(Buf,fs.size);
fs.Seek(0,soFromBeginning);
fs.ReadBuffer(Buf^,fs.Size);
RichEdit1.Lines.Add(DateToStr(Date)+' '+TimeToStr(Time)+'==>>'+
'共发送:'+IntToStr(cs.Socket.SendBuf(Buf^,fs.Size))+
'字节。' );
end;