可以实现,但也还有点问题,原因不明!文中有说明
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Unit2, ComCtrls, IdUDPServer, IdBaseComponent,
IdComponent, IdUDPBase, IdUDPClient, IdSocketHandle, RxRichEd,
IdTCPServer, IdTCPConnection, IdTCPClient;
type
TForm1 = class(TForm)
btnSend: TButton;
IdUDPClient1: TIdUDPClient; // 没使用
IdUDPServer1: TIdUDPServer;
RichEdit1_1: TRichEdit;
btnOpen: TButton;
RichEdit2_1: TRichEdit;
RxRichEdit1: TRxRichEdit;
RxRichEdit2: TRxRichEdit;
IdUDPServer2: TIdUDPServer;
procedure btnSendClick(Sender: TObject);
procedure btnOpenClick(Sender: TObject);
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
fReceiveMS :TMemoryStream; // 接收流
fbIsRecaive :boolean; // 是否正在接收
fiRecaiveSize,fiCurRecSize :Int64; // 将要接收总大小,已接收大小
end;
var
Form1: TForm1;
implementation
uses Math;
{$R *.dfm}
procedure TForm1.btnSendClick(Sender: TObject);
var
ms :TMemoryStream;
Buffer : array [0..1023] of Char;
iCount, iAllSize, iByteCount :Int64;
begin
ms := TMemoryStream.Create;
try
{IdUDPClient1.BufferSize := 8192;
IdUDPClient1.Host := '127.0.0.1';
IdUDPClient1.Port := 20001;
IdUDPClient1.Active := True;
IdUDPClient1.Send('StartSendData');} // 开始标志
IdUDPServer2.Send('127.0.0.1',20001,'StartSendData');
RxRichEdit1.Lines.SaveToStream(ms);
iAllSize := ms.Size;
//IdUDPClient1.SendBuffer(iAllSize,SizeOf(Int64)); // 大小
IdUDPServer2.SendBuffer('127.0.0.1',20001,iAllSize,SizeOf(Int64));
ms.Seek(0,soBeginning);
iCount := 0;
iByteCount := Min(ms.Size,SizeOf(Buffer));
while iByteCount >0 do
begin
ms.ReadBuffer(Buffer[0],iByteCount);
//IdUDPClient1.SendBuffer('127.0.0.1',20001,Buffer,iByteCount);
//IdUDPClient1.SendBuffer(Buffer,iByteCount);
IdUDPServer2.SendBuffer('127.0.0.1',20001,Buffer[0],iByteCount);
Inc(iCount,iByteCount);
iByteCount := Min(iAllSize-iCount,SizeOf(Buffer));
end;
// Todo: 分段数据是成功的,可是接收次数有时与这里不一致,搞不懂呀,搞不懂。
// 有时成功,有时不行!!!
finally
ms.Free;
end;
end;
procedure TForm1.btnOpenClick(Sender: TObject);
begin
with TOpenDialog.Create(nil) do
try
if Execute then
RxRichEdit1.Lines.LoadFromFile(FileName);
finally
Free;
end;
end;
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
iSize :integer;
Buffer:array [0..1023] of Char;
begin
if Not ((ABinding.IP = '127.0.0.1') and (ABinding.Port =20001)) then Exit;
iSize := AData.Size;
if (Not fbIsRecaive) and (iSize = 13) then // 开始标志
begin
AData.ReadBuffer(Buffer,iSize);
if StrLIComp(Buffer,'StartSendData',iSize)=0 then
begin
//fReceiveMS.Clear;
//fReceiveMS.Size := 0;
if Assigned(fReceiveMS) then FreeAndNil(fReceiveMS);
fReceiveMS := TMemoryStream.Create;
fiRecaiveSize := 0;
fiCurRecSize := 0;
fbIsRecaive := True;
Exit;
end;
end;
if fbIsRecaive and (iSize >0) then
begin
if fiRecaiveSize =0 then // 大小
begin
AData.Read(fiRecaiveSize,SizeOf(Int64));
Exit;
end;
AData.ReadBuffer(Buffer[0],iSize);
if Not Assigned(fReceiveMS) then Exit;
fReceiveMS.WriteBuffer(Buffer[0],iSize);
Inc(fiCurRecSize,iSize);
if fiCurRecSize >= fiRecaiveSize then // 结束
begin
fbIsRecaive := False;
fReceiveMS.Seek(0,soBeginning);
RxRichEdit2.Lines.LoadFromStream(fReceiveMS);
//fReceiveMS.Clear;
fiRecaiveSize :=0;
fiCurRecSize :=0;
FreeAndNil(fReceiveMS);
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//fReceiveMS := TMemoryStream.Create;
with IdUDPServer1.Bindings.Add do // 接收绑定 IP/Port
begin
IP := '127.0.0.1';
Port := 20001
end;
IdUDPServer1.ThreadedEvent := True;
IdUDPServer1.Active := True; // 接收
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
if Assigned(FreceiveMS) then FreeAndNil(fReceiveMS);
end;
end.