H
howardyin
Unregistered / Unconfirmed
GUEST, unregistred user!
我在写一个屏幕监视软件,用自己定义的UDP对象接收数据,但是收不到,可是如果换成TNMUDP组件在设计时设置好,就可以正常运行,受控端的代码应该没有问题.下面是主控端接收数据的代码,请大家帮忙看看有什么问题.是不是我的OnDataReceived函数挂错了?
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
NMUDP, ExtCtrls,jpeg;
const BufSize=2048;{ 发送每一笔数据的缓冲区大小 }
udpport=9494;{UDP端口}
type
TMYudp=class(TNMUDP)
procedure myudpDataReceived(Sender: TComponent; NumberBytes: Integer; FromIP: String; Port: Integer);
procedure myudpstatus(Sender: TComponent; status: string);
private
RsltStream,TmpStream:TMemoryStream;
public
constructor Create(aowner:tcomponent); override;
destructor Destroy; override;
end;
TForm2 = class(TForm)
Image1: TImage;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormShow(Sender: TObject);
private
public
{ Public declarations }
end;
var
Form2: TForm2; myudp1:tmyudp;
implementation
uses main;
{$R *.DFM}
procedure tmyudp.myudpstatus(Sender: TComponent; status: string);
begin
form1.Memo1.Lines.Add(status);
end;
procedure TmyUdp.myudpDataReceived(Sender: TComponent; NumberBytes: Integer; FromIP: String; Port: Integer);
var reqcode:array[0..9] of char;
jpegpic:tjpegimage;
begin
self.ReadStream(TmpStream);
RsltStream.CopyFrom(TmpStream,NumberBytes);
if NumberBytes< BufSize then { 数据已读完 }
begin
RsltStream.Position:=0;
jpegpic:=tjpegimage.create();//建立JPEG对象,用来装入JPEG图象
jpegpic.LoadFromStream(rsltstream);//装入JPEG
form2.image1.Picture.Assign(jpegpic);//把JPEG图象放到image1中显示出来
jpegpic.Free;
TmpStream.Clear;
RsltStream.Clear;
self.SendBuffer(reqcode,10);//继续要求发送下一帧屏幕图象
end
else
begin
TmpStream.Clear;
self.RemoteHost:=fromip;
self.SendBuffer(reqcode,10);
end;
end;
constructor TMYudp.Create(aowner:tcomponent);
begin
RemotePort:=udpport;
localport:=udpport;
RsltStream:=TMemoryStream.Create;
TmpStream:=TMemoryStream.Create;
OnDataReceived:=myudpDataReceived;
OnStatus:=myudpstatus;
end;
destructor TMYudp.Destroy;
begin
RsltStream.free;
TmpStream.free;
end;
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
myudp1.Destroy;
end;
procedure TForm2.FormShow(Sender: TObject);
begin
myudp1:=tmyudp.create(form2);
myudp1.RemoteHost:='192.168.0.2';
myudp1.RemotePort:=udpport;
end;
end.
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
NMUDP, ExtCtrls,jpeg;
const BufSize=2048;{ 发送每一笔数据的缓冲区大小 }
udpport=9494;{UDP端口}
type
TMYudp=class(TNMUDP)
procedure myudpDataReceived(Sender: TComponent; NumberBytes: Integer; FromIP: String; Port: Integer);
procedure myudpstatus(Sender: TComponent; status: string);
private
RsltStream,TmpStream:TMemoryStream;
public
constructor Create(aowner:tcomponent); override;
destructor Destroy; override;
end;
TForm2 = class(TForm)
Image1: TImage;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormShow(Sender: TObject);
private
public
{ Public declarations }
end;
var
Form2: TForm2; myudp1:tmyudp;
implementation
uses main;
{$R *.DFM}
procedure tmyudp.myudpstatus(Sender: TComponent; status: string);
begin
form1.Memo1.Lines.Add(status);
end;
procedure TmyUdp.myudpDataReceived(Sender: TComponent; NumberBytes: Integer; FromIP: String; Port: Integer);
var reqcode:array[0..9] of char;
jpegpic:tjpegimage;
begin
self.ReadStream(TmpStream);
RsltStream.CopyFrom(TmpStream,NumberBytes);
if NumberBytes< BufSize then { 数据已读完 }
begin
RsltStream.Position:=0;
jpegpic:=tjpegimage.create();//建立JPEG对象,用来装入JPEG图象
jpegpic.LoadFromStream(rsltstream);//装入JPEG
form2.image1.Picture.Assign(jpegpic);//把JPEG图象放到image1中显示出来
jpegpic.Free;
TmpStream.Clear;
RsltStream.Clear;
self.SendBuffer(reqcode,10);//继续要求发送下一帧屏幕图象
end
else
begin
TmpStream.Clear;
self.RemoteHost:=fromip;
self.SendBuffer(reqcode,10);
end;
end;
constructor TMYudp.Create(aowner:tcomponent);
begin
RemotePort:=udpport;
localport:=udpport;
RsltStream:=TMemoryStream.Create;
TmpStream:=TMemoryStream.Create;
OnDataReceived:=myudpDataReceived;
OnStatus:=myudpstatus;
end;
destructor TMYudp.Destroy;
begin
RsltStream.free;
TmpStream.free;
end;
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
myudp1.Destroy;
end;
procedure TForm2.FormShow(Sender: TObject);
begin
myudp1:=tmyudp.create(form2);
myudp1.RemoteHost:='192.168.0.2';
myudp1.RemotePort:=udpport;
end;
end.