function send_data(data_send:in_data):boolean;
var
this_connect:connect;
stream:TMemoryStream;
fs:tfilestream;
begin
this_connect:=find_id(data_send.con_id);
if this_connect.thread_id>0 then
begin
{
type
data_record=record
con_code:smallint;//操作码
con_id:integer;//发送目的id号
mess_len:integer;//消息长度
file_size:int64;//文件长度
name_size:smallint;//文件名的长度
filename:string;//文件名
mess:string;//消息体,文本格式
end;
}
stream:=TMemoryStream.Create;
stream.WriteBuffer(data_send.con_code,sizeof(data_send.con_code));
stream.WriteBuffer(data_send.con_id,sizeof(data_send.con_id));
if fileexists(data_send.filename) then
begin
data_send.name_size:=length(data_send.filename);
fs:=tfilestream.Create(data_send.filename,fmopenread);
data_send.file_size:=fs.Size;
stream.WriteBuffer(data_send.file_size,sizeof(data_send.file_size));//发送文件大小
stream.WriteBuffer(data_send.name_size,sizeof(data_send.name_size));//发送文件名大小
stream.WriteBuffer(data_send.filename[1],data_send.name_size);//发送文件名
stream.WriteBuffer(fs,data_send.file_size);//发送文件体
fs.Free;
end
else
begin
data_send.file_size:=0;
stream.WriteBuffer(data_send.file_size,sizeof(data_send.file_size));//发送文件大小0
end;
this_connect.objects.Connection.WriteStream(stream,true,true);
stream.Free;
result:=true;
end
else
result:=false;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FStream := nil;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
if Assigned(FStream) then
begin
FStream.Free;
FStream := nil;
end;
end;
procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
if Assigned(FStream) then
begin
FStream.Free;
FStream := nil;
end;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
FStream := TFileStream.Create('c:\temp\test.stream.html', fmCreate or fmShareDenyWrite);
end;
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Socket.SendStream(TFileStream.Create('c:\temp\test.html', fmOpenRead or fmShareDenyWrite));
end;