高手請過來看看,包裝UDP的問題(50分)

  • 主题发起人 主题发起人 hgptm
  • 开始时间 开始时间
H

hgptm

Unregistered / Unconfirmed
GUEST, unregistred user!
請見如下:

unit UDPData;
interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,NMUDP;

type
TReceiveDataEvent = procedure(Recstr:string) of object;

TDummy = class
class procedure onReceiveData(Sender: TComponent; NumberBytes: Integer; FromIP: String; Port: Integer);
end;

TMyUDP = Class
private
FOnReceiveData: TReceiveDataEvent;


public
NMUDP: TNMUDP;

gCurProcessStr: string;

procedure ReceiveData(Recstr:string);

constructor Create;
destructor Destroy; override;

Published

property OnReceiveData: TReceiveDataEvent read FOnReceiveData write FOnReceiveData;
end;




implementation

class Procedure TDummy.onReceiveData(Sender: TComponent; NumberBytes: Integer; FromIP: String; Port: Integer);
var ss: string;
buff: array[0..10] of char;
i: integer;
begin
(Sender as TNMUDP).ReadBuffer(buff, NumberBytes); //
ss:='';
for i:=0 to NumberBytes -1 do
ss:= ss + buff;


// 如何實現 : 將處理結果ss傳給TMyUdp.gCurProcessStr 賓 觸發TMyUdp的OnReceiveData事件????
gCurProcessStr := ss;
//.
end;


constructor TMyUDP.Create;
begin
inherited Create;

NMUDP := TNMUDP.Create(nil);

NMUDP.LocalPort := 8888;
NMUDP.RemotePort := 8080;
NMUDP.RemoteHost := '127.0.0.1';
NMUDP.OnDataReceived := TDummy.onReceiveData;

end;


destructor TMyUDP.Destroy;
begin
if NMUDP <> nil then
begin

FreeAndnil(NMUDP);
end;
inherited;

end;

procedure TMyUDP.ReceiveData(Recstr:string);
begin
if Assigned(FOnReceiveData) then FOnReceiveData(gCurProcessStr);
end;

end.
 
可能不一定完全理解楼主的意思。我看楼主要在处理事件的过程中,将ss值传递到TmyUpd类中吧?
如果是这样。建议楼主按下面的方式来实现:

主要思路是,实现OnReciveData事件直接可以在TMyUDP中啊。

//////////////////////////////////////////////////////////
unit Unit2;
interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, NMUDP;

type
TReceiveDataEvent = procedure(Recstr:string) of object;

TMyUDP = Class
private
procedure onReceiveData(Sender: TComponent; NumberBytes: Integer; FromIP: String; Port: Integer);
public
NMUDP: TNMUDP;
gCurProcessStr: string;
procedure ReceiveData(Recstr:string);
constructor Create;
destructor Destroy; override;
end;




implementation

constructor TMyUDP.Create;
begin
inherited Create;

NMUDP := TNMUDP.Create(nil);
NMUDP.LocalPort := 8888;
NMUDP.RemotePort := 8080;
NMUDP.RemoteHost := '127.0.0.1';
NMUDP.OnDataReceived := onReceiveData;
end;

destructor TMyUDP.Destroy;
begin
if NMUDP <> nil then
begin
FreeAndnil(NMUDP);
end;
inherited;
end;

procedure TMyUDP.onReceiveData(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
var
ss: string;
buff: array[0..10] of char;
i: integer;
begin
(Sender as TNMUDP).ReadBuffer(buff, NumberBytes); //
ss:='';
for i:=0 to NumberBytes -1 do
ss:= ss + buff;

// 这里!!!!!!!!!!!
gCurProcessStr := ss;
end;

procedure TMyUDP.ReceiveData(Recstr:string);
begin
if Assigned(FOnReceiveData) then FOnReceiveData(gCurProcessStr);
end;

end.
 
To: Xiammy兄
不錯,我的本意是:
在類TmyUDP中自動創建NMUDP:TNMUDP,且指定NMUDP.OnDataReceived 事件,然后將NMUDP接受到的結果傳遞給用戶. 讓用戶把NMUDP的接受事件當作"黑匣子",即用戶只是看到接受的結果(gCurProcessStr)。

你的代碼,我試下先,多謝不吝賜教.
此問題比較急,如果分少了,小弟再補!!!!!
 
補充一下:
TMyUDP.ReceiveData是必須要實現的,用于當gCurProcessStr<>''時就觸發的事件,相當于TNMUdp.OnDataReceived 事件.即用戶只是需要在TMyUDP.ReceiveData(Recstr:string)事件中取得接受結果:Recstr.
 
多人接受答案了。
 
后退
顶部