用流,用内存流读入文件,然后转化成字符串流,然后发送,接受时先用字符串流接受,然后转化成内存流,再保存文件。
var
MStrm:TMemoryStream;
SStrm:TStringStream;
tmpText,lOrderText:WideString;
i:integer;
begin
//传送语音文件
try
label1.Caption :='发送文件. . . .';
application.ProcessMessages;
lOrderText:=SendNotify+';;;';
g_NetMgr.SendOrder(g_RemoteIP,g_RemotePort,lOrderText); //通知平台开始接受
MStrm:=TMemoryStream.Create;
Mstrm.LoadFromFile(ExtractFilePath(Application.ExeName)+'/Notify/Notify.tw');
SStrm:=TStringStream.Create('');
MStrm.SaveToStream(SStrm);
tmpText:= SStrm.DataString ;
while length(tmpText)>0 do
begin
if length(tmpText)>=2048 then
begin
lOrderText := NotifyFile +';'+copy(tmpText,1,2048)+';;';
delete(tmpText,1,2048);
end
else
begin
if length(tmpText) >0 then
lOrderText := NotifyFile +';'+tmpText+';;';
tmpText:='';
end;
g_NetMgr.SendOrder(g_RemoteIP,g_RemotePort,lOrderText); //发送文件
application.ProcessMessages;
sleep(10);
end;
finally
lOrderText:=SendNotifyOver+';;;';
g_NetMgr.SendOrder(g_RemoteIP,g_RemotePort,lOrderText); //清除语音平台缓冲中的数据
FreeAndNil(MStrm);
FreeAndNil(SStrm);
end;
///
function TNetMgrForm.SendOrder(AOrderIP: string;AOrderPort:integer; AOrderText: string):boolean;
begin
// result := false;
try
if not IdUDPClient1.Active then
IdUDPClient1.Active := true;
IdUDPClient1.Send(AOrderIP,AOrderPort,inttostr(IdUDPServer1.DefaultPort)+';'+AOrderText);
end;
//接受时
var
lOrderName,lOrderPara,lOrderCallBack: widestring;
lResultAskSend,lResultPort: integer;
begin
lResultPort := strtoint(cut_str(0,1,';',AOrderText));
lOrderName := cut_str(1,2,';',AOrderText);
lOrderPara := cut_str(2,3,';',AOrderText);
lOrderCallBack := cut_str(3,4,';',AOrderText);
if lOrderName = TEST then
SendOrder(AOrderIP,lResultPort,RESULTTEST)
else if lOrderName = ADDTASK then
ProcessTaskOrder(AOrderIP,lResultPort,ADDTASK,lOrderPara,lOrderCallBack)
else if lOrderName = ASKSEND then //请求发送
begin
lResultAskSend := g_ChannelMgr.WorkedChannelCount - g_ChannelMgr.Tasklist.Count;
SendOrder(AOrderIP,lResultPort,RESULTASKSEND+';'+inttostr(lResultAskSend)+';');
end
else if lOrderName = CLEARTASK then
g_ChannelMgr.ClearTaskOrder(AOrderIP)
else if lOrderName = SendNotify then //准备接受数据
begin
g_NotifyStr:='';
end
else if lOrderName = SendNotifyOver then //完成接受数据
begin
if length(g_NotifyStr) <0 then exit;
g_MStrm:=TMemoryStream.Create ;
g_SStrm:=TStringStream.Create(g_NotifyStr);
g_MStrm.LoadFromStream(g_SStrm);
g_Mstrm.SaveToFile(g_VoicePath+'/Notify.TW');
FreeAndNil(g_MStrm);
FreeAndNil(g_SStrm);
g_NotifyStr:='';
end
else if lOrderName = NotifyFile then //保存语音文件
begin
g_NotifyStr:=g_NotifyStr+StrNotify ; //g_NotifyStr是全局变量
end
else if lOrderName = RESULTDATA then
if not g_ChannelMgr.AddResultData(lOrderPara,lOrderCallBack) then
memo1.Lines.Add('返回数据错误: ['+AOrderIP+':'+inttostr(AOrderPort)+'] <-- '+AOrderText);
//application.ProcessMessages;
end;
//代码是我剪切程序中的,传输时的各命令都是自定义的