如果是稳定的局域网就可以直接使用Indy中的UDP控件。例子可以参考下面这个。
服务端最后使用了一个TFilestream,将接收的数据保存为一个文件了。稍做修改就可满足你的要求了。
客户端程序:
unit clientUnit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdAntiFreezeBase, IdAntiFreeze, IdBaseComponent, IdComponent,
IdUDPBase, IdUDPClient, ExtCtrls,jpeg;
type
Tfrmclient = class(TForm)
Timer1: TTimer;
IdUDPClient1: TIdUDPClient;
IdAntiFreeze1: TIdAntiFreeze;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmclient: Tfrmclient;
Fstream : TMemorystream;
implementation
const bufsize=4096-1;
{$R *.dfm}
procedure Tfrmclient.FormCreate(Sender: TObject);
begin
Fstream:=TMemorystream.Create;
Timer1.Interval:=30000;
idudpclient1.Host:='172.16.88.3';
idudpclient1.Port:=9090;
// idudpclient1.Active:=true;
end;
procedure Tfrmclient.FormDestroy(Sender: TObject);
begin
Fstream.Free;
end;
procedure Tfrmclient.Timer1Timer(Sender: TObject);
var
receivedstring,filename:string;
dc:hdc;
fjpeg:tjpegimage;
fscreencanvas:tcanvas;
fbitmap:Tbitmap;
buf:array[0..bufsize] of char;
sendlen:integer;
intoffset:int64;
sRect,Drect:TRect;
begin
try
filename:=formatdatetime('yyyymmddhhnnsszzz',now);
idudpclient1.Send('send'+filename);
receivedstring:=idudpclient1.ReceiveString();
if uppercase(receivedstring)='OK' then
begin
dc:=getdc(0);
fScreencanvas:=Tcanvas.Create;
Fscreencanvas.Handle:=dc;
fbitmap:=tbitmap.Create;
fbitmap.Width:=screen.Width;
fbitmap.Height:=screen.Height;
sRect:=Rect(0,0,screen.Width,screen.Height);
dRect:=Rect(0,0,screen.Width,screen.Height);
fbitmap.Canvas.CopyRect(sRect,fscreencanvas,dRect);
fjpeg:=Tjpegimage.Create;
fjpeg.Assign(fbitmap);
fjpeg.CompressionQuality:=8;
fjpeg.SaveToStream(fstream);
fstream.Position:=0;
// if fstream.Size<0 then exit;
intoffset:=0;
while true do
begin
if intoffset>=fstream.Size then break;
fstream.Position:=intoffset;
sendlen:=fstream.Read(buf,sizeof(buf));
idudpclient1.SendBuffer(buf,sendlen);
application.ProcessMessages;
intoffset:=intoffset+sendlen;
end;
end;
releasedc(0,dc);
fbitmap.Free;
fjpeg.Free;
fstream.Clear;
idudpclient1.Send('end');
except
//idudpclient1.Send('end');
end;
end;
end.
服务端程序:
unit serverUnit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdAntiFreezeBase, IdAntiFreeze, IdBaseComponent, IdComponent,
IdUDPBase, IdUDPServer, StdCtrls,IdSocketHandle;
type
Tfrmserver = class(TForm)
Button1: TButton;
IdUDPServer1: TIdUDPServer;
IdAntiFreeze1: TIdAntiFreeze;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmserver: Tfrmserver;
mem : Tfilestream;
implementation
{$R *.dfm}
procedure Tfrmserver.Button1Click(Sender: TObject);
begin
idudpserver1.DefaultPort:=9090;
idudpserver1.Active:=true;
end;
procedure Tfrmserver.Button2Click(Sender: TObject);
begin
idudpserver1.Active:=false;
end;
procedure Tfrmserver.FormDestroy(Sender: TObject);
begin
if mem<>nil then mem.Free;
mem:=nil;
end;
procedure Tfrmserver.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
str,filename:string;
begin
adata.Seek(0,0);
setlength(str,adata.size);
adata.Read(str[1],adata.Size);
if uppercase(str)='END' then
if mem<>nil then begin mem.free;mem:=nil;exit; end;
if pos('SEND',uppercase(str))>0 then
begin
filename:=str;
delete(filename,1,length('SEND'));
filename:='d:/temp/'+abinding.PeerIP+'-'+filename+'.jpg';
if fileexists(filename) then deletefile(filename);
str:='OK';
Abinding.SendTo(abinding.PeerIP,abinding.PeerPort,str[1],length(str));
if mem<>nil then
begin
mem.free;
mem:=nil;
end;
if not fileexists(filename) then
mem:=TFilestream.Create(filename,fmopenreadwrite or fmcreate)
else
mem:=TFilestream.Create(filename,fmopenreadwrite);
end
else
if uppercase(str)<>'END' THEN
begin
if mem<>nil then
begin
adata.Seek(0,0);
mem.CopyFrom(adata,adata.Size);
end;
end;
end;