我给你一段我接收流文件的例子,你可以参考一下:
private (type form部分定义变量)
Buffer:array[0..9999] of char ;
SerStatus:TSerstatus;
B_FileLen:integer;
procedure TForm1.FormCreate(Sender: TObject);
begin
Serstatus:=csIdle; //初始化serstatus为csIdle
end;
procedure TForm1.SerSockClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
StrIn:string;
B_stream:TmemoryStream;
B_StrLen,B_position:integer;
Time1:cardinal;
begin
case SerStatus of
csIdle:
begin
Socket.ReCeiveBuf(Buffer,6); //我所传过来的文件的头6位是文件信息而非内容
Strin:=Copy(Buffer,1,6);
if Copy(Strin,1,4)='WJJS' then //取文件住息中头四位
begin
SerStatus:=CsLeng;
B_FileLen:=StrToInt(Copy(Strin,5,2)); //取文件名长度
end;
end;
csLeng:
begin
Socket.ReceiveBuf(Buffer,B_FileLen); //这些是文件名信息
Strin:=Copy(Buffer,1,B_FileLen); //取文件名
if copy(Strin,1,2)='Db' then
begin
SerStatus:=csFile; //设置状态以便取内容
B_CanHttp:=False;
Lsb_Js.Items.Add('正在接收文件'+StrIn+'... '+TimeToStr(Time));
end;
end;
csFile:
begin
B_Stream:=TMemoryStream.Create;
B_position:=10000;
While True do
begin
while Socket.ReceiveLength<10000 do
begin
if Socket.ReceiveLength=B_position then break;
B_position:=Socket.ReceiveLength ;
Time1:=GetTickCount;
While GetTickCount-Time1<200 do;
end;
B_StrLen:=Socket.ReceiveBuf(Buffer,Sizeof(Buffer));
if B_StrLen<=0 then
break
else B_Stream.Write(Buffer,B_StrLen);
end;
B_Stream.Position:=0;
B_Stream.SaveToFile(ExtractFilePath(Application.ExeName)+'Dbbb.mdb');
Lsb_Js.Items.Add('文件接收结束,正在保存文件... '+TimeToStr(Time));
B_Stream.Free;
SerStatus:=CsIdle;
Socket.SendText('RECOK!');
B_CanHttp:=True;
if copy(Strin,1,4)='Dbbb' then P_SaveSj
else P_SaveFile;
end;
end;
end;
代码里的一些控件无法一一指出,你看明白就行了。