to 张无忌,:
恩,我设置的buffersize是2048刚好2k,而且中间也sleep了却还是不行,现在全部代码在下面
您如果有空的话就帮忙看看
发送端
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, NMUDP, StdCtrls, Psock;
type
TForm1 = class(TForm)
CUDP: TNMUDP;
Label1: TLabel;
Timer1: TTimer;
Label2: TLabel;
Powersock1: TPowersock;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ScreenCap(LeftPos,TopPos,RightPos,BottomPos:integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
BmpStream:TMemoryStream;
BroadCastIP:string;
LeftSize:Longint;{ 发送每一笔数据后剩余的字节数 }
implementation
{$R *.DFM}
const BufSize=2048;{ 发送每一笔数据的缓冲区大小 }
procedure TForm1.FormCreate(Sender: TObject);
var
SendSize:integer;
Buf:array[0..BufSize-1] of char;
i,j,iHead:Integer;
LocalIP,sHead,s:String;
ai:array [1..3] of integer;
begin
Application.Minimize;
BmpStream:=TMemoryStream.Create;
LocalIP:=Powersock1.LocalIP;//本机ip
BroadCastIP:=LocalIP;
CUDP.RemoteHost:=BroadCastIP; { BroadCastIP为广播地址 }
if BmpStream.Size=0 then { 没有数据可发,必须截屏生成数据 }
ScreenCap(0,0,100,100); {截取屏幕 }
while LeftSize>BufSize do
begin
SendSize:=BufSize;
BmpStream.ReadBuffer(Buf,SendSize);
//把要发送的数据读入buf中
LeftSize:=LeftSize-SendSize;
//计算剩余的数据
if LeftSize=0 then
begin
BmpStream.Clear;{如果剩余数据为空,清空流 }
exit;
end;
CUDP.SendBuffer(Buf,SendSize); { 将数据发到主控机的2222口 }
sleep(100);
end;
SendSize:=LeftSize;
BmpStream.ReadBuffer(Buf,SendSize);
LeftSize:=LeftSize-SendSize;
CUDP.SendBuffer(Buf,SendSize); { 将数据发到主控机的2222口 }
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
BmpStream.Free;
end;
procedure TForm1.ScreenCap(LeftPos, TopPos, RightPos, BottomPos: integer);
var
RectWidth,RectHeight:integer;
SourceDC,DestDC,Bhandle:integer;
Bitmap:TBitmap;
begin
Application.Minimize;
RectWidth:=RightPos-LeftPos;
RectHeight:=BottomPos-TopPos;
SourceDC:=CreateDC('DISPLAY','','',nil);
DestDC:=CreateCompatibleDC(SourceDC);
Bhandle:=CreateCompatibleBitmap(SourceDC,RectWidth,RectHeight);
SelectObject(DestDC,Bhandle);
BitBlt(DestDC,0,0,RectWidth,RectHeight,SourceDC,LeftPos,TopPos,SRCCOPY);
Bitmap:=TBitmap.Create;
Bitmap.Handle:=BHandle;
BitMap.SaveToStream(BmpStream);
BmpStream.Position:=0;
LeftSize:=BmpStream.Size;
Bitmap.Free;
DeleteDC(DestDC);
ReleaseDC(Bhandle,SourceDC);
end;
end.
接收端
unit server;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
NMUDP, StdCtrls, ExtCtrls, Psock;
type
TForm1 = class(TForm)
SUDP: TNMUDP;
Image1: TImage;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Powersock1: TPowersock;
Label2: TLabel;
Button1: TButton;
Label3: TLabel;
procedure FormCreate(Sender: TObject);
procedure SUDPDataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
RsltStream,TmpStream:TMemoryStream;
temp:integer;
implementation
{$R *.DFM}
const BufSize=2048;
procedure TForm1.FormCreate(Sender: TObject);
begin
RsltStream:=TMemoryStream.Create;
TmpStream:=TMemoryStream.Create;
MyStream1:=TMemoryStream.Create;
end;
procedure TForm1.SUDPDataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
begin
SUDP.ReadStream(TmpStream);
RsltStream.CopyFrom(TmpStream,NumberBytes);
if NumberBytes<BufSize then
begin
RsltStream.Position:=0;
Image1.Picture.Bitmap.LoadFromStream(RsltStream);
TmpStream.Clear;
RsltStream.Clear;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
RsltStream.Free;
TmpStream.Free;
end;
end.