===============服务端================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer,idsockethandle,
Gauges;
type
TForm1 = class(TForm)
IdUDPServer1: TIdUDPServer;
SaveDialog1: TSaveDialog;
Gauge1: TGauge;
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
FileName:string;//保存收到的文件名
FileSize:integer;//文件的找度
mem:tFileStream;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
str:string;
begin
aData.Seek(0,0);
setLength(str,aData.size);
aData.Read(str[1],aData.Size);
if pos('Send File:File name:',str)>0 then begin
delete(str,1,length('Send File:File name:'));
fileName:=str;
//showmessage(str);
str:='Received File ok';
abinding.SendTo(aBinding.PeerIP,aBindIng.PeerPort,str[1],length(str));
// idUDPserver1.Send(idUDPServer1.);
end else if pos('File Length:',str)>0 then begin
delete(str,1,length('File Length:'));
FileSize:=strToIntDef(str,0);
SaveDialog1.FileName:=fileName;
if saveDialog1.Execute then begin
FileName:=saveDialog1.FileName;
if FileExists(filename) then
deleteFile(filename);
if mem<>nil then begin
mem.Free;
mem:=nil;
end;
if not fileExists(FileName) then
mem:=TFileStream.Create(FileName,sysutils.fmOpenReadWrite or fmCreate)
else
mem:=TFileStream.Create(FileName,fmopenReadWrite);
str:='get file Length ok';
aBinding.SendTo(aBinding.PeerIP,aBinding.PeerPort,str[1],length(str));
end else begin //saveDialog1.execute
str:='received File cancel';
aBinding.SendTo(aBinding.PeerIP,aBinding.PeerPort,str[1],length(str));
end;
end else if pos('File Send End',str)>0 then begin
if mem<>nil then begin
mem.Free;
mem:=nil;
str:='Received File Successed';
aBinding.SendTo(aBinding.PeerIP,aBinding.PeerPort,str[1],length(str));
filename:='';
fileSize:=0;
end;
end else begin
if mem<>nil then begin
mem.Seek(0,2);
aData.Seek(0,0);
mem.CopyFrom(aData,aData.Size);
gauge1.Progress:=round(mem.size/fileSize*100);
str:='received ok';
abinding.SendTo(aBinding.PeerIP,aBinding.PeerPort,str[1],length(str));
application.ProcessMessages;
end;
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if mem<>nil then
mem.Free;
end;
end.
=====================================
===============客户端================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Gauges, IdBaseComponent, IdComponent, IdUDPBase,
IdUDPClient;
type
TForm1 = class(TForm)
Edit1: TEdit;
IdUDPClient1: TIdUDPClient;
Gauge1: TGauge;
Button1: TButton;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
receivedString:string;
mem:tfileStream;
posi,len:integer;
p:array [0..1023] of byte;
begin
if opendialog1.Execute then begin
idUDPClient1.Host:=edit1.Text;
idUDPClient1.Active:=true;
idUDPClient1.Send('Send File:File name:'+opendialog1.FileName);
ReceivedString:=idUDPClient1.ReceiveString;
if uppercase(Receivedstring)=upperCase('Received File ok') then
begin
//showmessage('服务器提示收到文件名');
mem:=tfileStream.Create(opendialog1.FileName,fmOpenRead);
try
posi:=0;
idUDPclient1.Send('File Length:'+IntToStr(mem.Size));
receivedString:=idUDPClient1.ReceiveString();
if uppercase(receivedString)=upperCase('get file Length ok') then begin
while posi<mem.Size do begin
len:=1024; //只能发 1024
if mem.Size<1024 then //如果长度不到 1024
len:=mem.Size;
mem.Read(p,len);
idUDPClient1.SendBuffer(p,len);
inc(posi,len);
gauge1.Progress:=round(posi/mem.size*100);
receivedString:=idUDPClient1.ReceiveString();
if upperCase(ReceivedString)<>upperCase('received ok') then
break;
application.ProcessMessages;
end; //end while
idUDPClient1.Send('File Send End');
if idUdpClient1.ReceiveString()='Received File Successed' then
showmessage('文件成功发送');
end else showmessage('cancel');// end while
//end 取得文件长度
finally
mem.Free;
end;
end;
end;//opendialog1.execute
end;
end.
=====================================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer,idsockethandle,
Gauges;
type
TForm1 = class(TForm)
IdUDPServer1: TIdUDPServer;
SaveDialog1: TSaveDialog;
Gauge1: TGauge;
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
FileName:string;//保存收到的文件名
FileSize:integer;//文件的找度
mem:tFileStream;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
str:string;
begin
aData.Seek(0,0);
setLength(str,aData.size);
aData.Read(str[1],aData.Size);
if pos('Send File:File name:',str)>0 then begin
delete(str,1,length('Send File:File name:'));
fileName:=str;
//showmessage(str);
str:='Received File ok';
abinding.SendTo(aBinding.PeerIP,aBindIng.PeerPort,str[1],length(str));
// idUDPserver1.Send(idUDPServer1.);
end else if pos('File Length:',str)>0 then begin
delete(str,1,length('File Length:'));
FileSize:=strToIntDef(str,0);
SaveDialog1.FileName:=fileName;
if saveDialog1.Execute then begin
FileName:=saveDialog1.FileName;
if FileExists(filename) then
deleteFile(filename);
if mem<>nil then begin
mem.Free;
mem:=nil;
end;
if not fileExists(FileName) then
mem:=TFileStream.Create(FileName,sysutils.fmOpenReadWrite or fmCreate)
else
mem:=TFileStream.Create(FileName,fmopenReadWrite);
str:='get file Length ok';
aBinding.SendTo(aBinding.PeerIP,aBinding.PeerPort,str[1],length(str));
end else begin //saveDialog1.execute
str:='received File cancel';
aBinding.SendTo(aBinding.PeerIP,aBinding.PeerPort,str[1],length(str));
end;
end else if pos('File Send End',str)>0 then begin
if mem<>nil then begin
mem.Free;
mem:=nil;
str:='Received File Successed';
aBinding.SendTo(aBinding.PeerIP,aBinding.PeerPort,str[1],length(str));
filename:='';
fileSize:=0;
end;
end else begin
if mem<>nil then begin
mem.Seek(0,2);
aData.Seek(0,0);
mem.CopyFrom(aData,aData.Size);
gauge1.Progress:=round(mem.size/fileSize*100);
str:='received ok';
abinding.SendTo(aBinding.PeerIP,aBinding.PeerPort,str[1],length(str));
application.ProcessMessages;
end;
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if mem<>nil then
mem.Free;
end;
end.
=====================================
===============客户端================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Gauges, IdBaseComponent, IdComponent, IdUDPBase,
IdUDPClient;
type
TForm1 = class(TForm)
Edit1: TEdit;
IdUDPClient1: TIdUDPClient;
Gauge1: TGauge;
Button1: TButton;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
receivedString:string;
mem:tfileStream;
posi,len:integer;
p:array [0..1023] of byte;
begin
if opendialog1.Execute then begin
idUDPClient1.Host:=edit1.Text;
idUDPClient1.Active:=true;
idUDPClient1.Send('Send File:File name:'+opendialog1.FileName);
ReceivedString:=idUDPClient1.ReceiveString;
if uppercase(Receivedstring)=upperCase('Received File ok') then
begin
//showmessage('服务器提示收到文件名');
mem:=tfileStream.Create(opendialog1.FileName,fmOpenRead);
try
posi:=0;
idUDPclient1.Send('File Length:'+IntToStr(mem.Size));
receivedString:=idUDPClient1.ReceiveString();
if uppercase(receivedString)=upperCase('get file Length ok') then begin
while posi<mem.Size do begin
len:=1024; //只能发 1024
if mem.Size<1024 then //如果长度不到 1024
len:=mem.Size;
mem.Read(p,len);
idUDPClient1.SendBuffer(p,len);
inc(posi,len);
gauge1.Progress:=round(posi/mem.size*100);
receivedString:=idUDPClient1.ReceiveString();
if upperCase(ReceivedString)<>upperCase('received ok') then
break;
application.ProcessMessages;
end; //end while
idUDPClient1.Send('File Send End');
if idUdpClient1.ReceiveString()='Received File Successed' then
showmessage('文件成功发送');
end else showmessage('cancel');// end while
//end 取得文件长度
finally
mem.Free;
end;
end;
end;//opendialog1.execute
end;
end.
=====================================