delphi怎么接收组播
delphi怎么接收组播啊?最好给个详细的例子谢谢!最好给一个使用idudpserver的例子...
delphi怎么接收组播啊?最好给个详细的例子 谢谢!
最好给一个使用idudpserver的例子 展开
最好给一个使用idudpserver的例子 展开
5个回答
展开全部
楼主说的组播是什么,UDP包吗. indy控件的简单使用貌似没有什么太多的技术含量啊, 申明两个udpclient就可以直接对发
我把我的程序的代码贴一些上来,楼主自己整理一下吧,希望有点帮助。
这是一个用UDP搜索局域网的主机的程序
TUDPSearch = class(TObject)
private
FUDP: TIdUDPServer;
FSearchPort: Integer;
FLocalPort: integer;
FAimList: TStringList;
FOnLog: TLogEvent;
FOnFound: TFoundEvent;
function GetActive: boolean;
procedure SetActive(AValue: boolean);
function GetAimCount: Integer;
function GetPort: Integer;
procedure SetPort(const Value: Integer);
protected
procedure DoRead(Sender: TObject; AData: TIdBytes; ABinding: TIdSocketHandle);
procedure AddListItem(AIP: string);
procedure ClearListItem;
procedure DoLog(AStr: string);
procedure DoFound(AIP: string);
public
constructor Create(APort: integer; bRun: boolean = False);
destructor Destroy; override;
procedure SetUDPReadEvent(AUDPReadEvent: TUDPReadEvent);
procedure Search; overload;
procedure Search(ABeginPort, AEndPort: integer); overload;
procedure Search(APort: Integer); overload;
procedure Send(ABinding: TIdSocketHandle; AContent: string);
function Run: boolean;
function Stop: boolean;
function GetListAsText: string;
property Port: integer read GetPort write SetPort;
property Active: Boolean read GetActive write SetActive;
property AimList: TStringList read FAimList write FAimList;
property AimCount: integer read GetAimCount;
property OnLog: TLogEvent read FOnLog write FOnLog;
property OnFound: TFoundEvent read FOnFound write FOnFound;
property SearchPort: integer read FSearchPort write FSearchPort;
property LocalPort: integer read FLocalPort write FLocalPort;
end;
procedure TUDPSearch.AddListItem(AIP: string);
begin
if FAimList.IndexOf(AIP) < 0 then
begin
FAimList.Add(AIP);
DoFound(AIP);
end;
end;
procedure TUDPSearch.ClearListItem;
begin
FAimList.Clear;
end;
constructor TUDPSearch.Create(APort: integer; bRun: boolean = False);
begin
FUDP := TIdUDPServer.Create(nil);
FAimList := TStringList.Create;
FUDP.BroadcastEnabled := True;
FUDP.OnUDPRead := DoRead;
FUDP.DefaultPort := APort;
FLocalPort := APort - 1;
FUDP.Active := bRun;
end;
destructor TUDPSearch.Destroy;
begin
try
FUDP.Active := False;
FUDP.Free;
FAimList.Free;
except
end;
inherited;
end;
function TUDPSearch.GetActive: boolean;
begin
Result := FUDP.Active;
end;
function TUDPSearch.GetAimCount: Integer;
begin
Result := FAimList.Count;
end;
function TUDPSearch.GetListAsText: string;
begin
if FAimList.Count > 0 then
result := FAimList.Text
else
Result := '列表为空';
end;
function TUDPSearch.Run: boolean;
begin
FUDP.Active := True;
Result := FUDP.Active;
end;
procedure TUDPSearch.Search(ABeginPort, AEndPort: integer);
var
i: integer;
begin
for I := ABeginPort to AEndPort do
Search(I);
end;
procedure TUDPSearch.SetActive(AValue: boolean);
begin
if FUDP.Active <> Avalue then
FUDP.Active := Avalue;
end;
procedure TUDPSearch.Search;
begin
Search(FSearchPort);
end;
procedure TUDPSearch.Search(APort: Integer);
begin
FAimList.Clear;
if APort = 0 then Exit;
DoLog(Format('向端口%d广播', [APort]));
FUDP.Broadcast(Format('%s:%d', [SearchAim, FLocalPort]), APort);
end;
procedure TUDPSearch.Send(ABinding: TIdSocketHandle; AContent: string);
begin
FUDP.Send(ABinding.PeerIP, ABinding.PeerPort, AContent);
end;
procedure TUDPSearch.DoFound(AIP: string);
begin
if Assigned(FOnFound) then
FOnFound(AIP);
end;
procedure TUDPSearch.DoLog(AStr: string);
begin
if Assigned(FOnLog) then
FOnLog(AStr);
end;
procedure TUDPSearch.DoRead(Sender: TObject; AData: TIdBytes; ABinding: TIdSocketHandle);
var
tmpStr: string;
APort: Integer;
CanAdd: boolean;
function GetPortFromData(Astr: string): Integer;
begin
result := StrToIntdef(Copy(Astr, pos(':', Astr) + 1, Length(Astr) - pos(':', Astr)), 0);
end;
begin
CanAdd := false;
APort := 0;
tmpStr := PChar(AData);
DoLog(Format('收到来自%s:%d的消息: %s', [ABinding.PeerIP, ABinding.PeerPort, tmpStr]));
if (Pos(SearchAim, tmpStr) > 0) and (tmpStr[Length(SearchAim)+1] = ':') then
begin
APort := GetPortFromData(tmpStr);
if APort > 0 then
CanAdd := True;
tmpStr := SearchAim;
end
else
if (Pos(AimAnwser, tmpStr) > 0) and (tmpStr[Length(AimAnwser)+1] = ':') then
begin
APort := GetPortFromData(tmpStr);
if APort > 0 then
CanAdd := True;
tmpStr := AimAnwser;
end;
if CanAdd then
begin
AddListItem(Format('%s:%d', [ABinding.PeerIP, APort]));
if tmpStr = SearchAim then
FUDP.Send(ABinding.PeerIP, ABinding.PeerPort, Format('%s:%d', [AimAnwser, LocalPort]));
end;
end;
function TUDPSearch.GetPort: Integer;
begin
Result := FUDP.DefaultPort;
end;
procedure TUDPSearch.SetPort(const Value: Integer);
begin
if FUDP.Active then
begin
raise Exception.Create('服务已开启TUDPSearch.SetPort调用失败');
Exit; //在运行时不能改
end;
if FUDP.DefaultPort <> Value then
begin
FUDP.DefaultPort := Value;
end;
end;
procedure TUDPSearch.SetUDPReadEvent(AUDPReadEvent: TUDPReadEvent);
begin
FUDP.OnUDPRead := AUDPReadEvent;
end;
function TUDPSearch.Stop: boolean;
begin
FUDP.Active := False;
Result := not FUDP.Active;
end;
我把我的程序的代码贴一些上来,楼主自己整理一下吧,希望有点帮助。
这是一个用UDP搜索局域网的主机的程序
TUDPSearch = class(TObject)
private
FUDP: TIdUDPServer;
FSearchPort: Integer;
FLocalPort: integer;
FAimList: TStringList;
FOnLog: TLogEvent;
FOnFound: TFoundEvent;
function GetActive: boolean;
procedure SetActive(AValue: boolean);
function GetAimCount: Integer;
function GetPort: Integer;
procedure SetPort(const Value: Integer);
protected
procedure DoRead(Sender: TObject; AData: TIdBytes; ABinding: TIdSocketHandle);
procedure AddListItem(AIP: string);
procedure ClearListItem;
procedure DoLog(AStr: string);
procedure DoFound(AIP: string);
public
constructor Create(APort: integer; bRun: boolean = False);
destructor Destroy; override;
procedure SetUDPReadEvent(AUDPReadEvent: TUDPReadEvent);
procedure Search; overload;
procedure Search(ABeginPort, AEndPort: integer); overload;
procedure Search(APort: Integer); overload;
procedure Send(ABinding: TIdSocketHandle; AContent: string);
function Run: boolean;
function Stop: boolean;
function GetListAsText: string;
property Port: integer read GetPort write SetPort;
property Active: Boolean read GetActive write SetActive;
property AimList: TStringList read FAimList write FAimList;
property AimCount: integer read GetAimCount;
property OnLog: TLogEvent read FOnLog write FOnLog;
property OnFound: TFoundEvent read FOnFound write FOnFound;
property SearchPort: integer read FSearchPort write FSearchPort;
property LocalPort: integer read FLocalPort write FLocalPort;
end;
procedure TUDPSearch.AddListItem(AIP: string);
begin
if FAimList.IndexOf(AIP) < 0 then
begin
FAimList.Add(AIP);
DoFound(AIP);
end;
end;
procedure TUDPSearch.ClearListItem;
begin
FAimList.Clear;
end;
constructor TUDPSearch.Create(APort: integer; bRun: boolean = False);
begin
FUDP := TIdUDPServer.Create(nil);
FAimList := TStringList.Create;
FUDP.BroadcastEnabled := True;
FUDP.OnUDPRead := DoRead;
FUDP.DefaultPort := APort;
FLocalPort := APort - 1;
FUDP.Active := bRun;
end;
destructor TUDPSearch.Destroy;
begin
try
FUDP.Active := False;
FUDP.Free;
FAimList.Free;
except
end;
inherited;
end;
function TUDPSearch.GetActive: boolean;
begin
Result := FUDP.Active;
end;
function TUDPSearch.GetAimCount: Integer;
begin
Result := FAimList.Count;
end;
function TUDPSearch.GetListAsText: string;
begin
if FAimList.Count > 0 then
result := FAimList.Text
else
Result := '列表为空';
end;
function TUDPSearch.Run: boolean;
begin
FUDP.Active := True;
Result := FUDP.Active;
end;
procedure TUDPSearch.Search(ABeginPort, AEndPort: integer);
var
i: integer;
begin
for I := ABeginPort to AEndPort do
Search(I);
end;
procedure TUDPSearch.SetActive(AValue: boolean);
begin
if FUDP.Active <> Avalue then
FUDP.Active := Avalue;
end;
procedure TUDPSearch.Search;
begin
Search(FSearchPort);
end;
procedure TUDPSearch.Search(APort: Integer);
begin
FAimList.Clear;
if APort = 0 then Exit;
DoLog(Format('向端口%d广播', [APort]));
FUDP.Broadcast(Format('%s:%d', [SearchAim, FLocalPort]), APort);
end;
procedure TUDPSearch.Send(ABinding: TIdSocketHandle; AContent: string);
begin
FUDP.Send(ABinding.PeerIP, ABinding.PeerPort, AContent);
end;
procedure TUDPSearch.DoFound(AIP: string);
begin
if Assigned(FOnFound) then
FOnFound(AIP);
end;
procedure TUDPSearch.DoLog(AStr: string);
begin
if Assigned(FOnLog) then
FOnLog(AStr);
end;
procedure TUDPSearch.DoRead(Sender: TObject; AData: TIdBytes; ABinding: TIdSocketHandle);
var
tmpStr: string;
APort: Integer;
CanAdd: boolean;
function GetPortFromData(Astr: string): Integer;
begin
result := StrToIntdef(Copy(Astr, pos(':', Astr) + 1, Length(Astr) - pos(':', Astr)), 0);
end;
begin
CanAdd := false;
APort := 0;
tmpStr := PChar(AData);
DoLog(Format('收到来自%s:%d的消息: %s', [ABinding.PeerIP, ABinding.PeerPort, tmpStr]));
if (Pos(SearchAim, tmpStr) > 0) and (tmpStr[Length(SearchAim)+1] = ':') then
begin
APort := GetPortFromData(tmpStr);
if APort > 0 then
CanAdd := True;
tmpStr := SearchAim;
end
else
if (Pos(AimAnwser, tmpStr) > 0) and (tmpStr[Length(AimAnwser)+1] = ':') then
begin
APort := GetPortFromData(tmpStr);
if APort > 0 then
CanAdd := True;
tmpStr := AimAnwser;
end;
if CanAdd then
begin
AddListItem(Format('%s:%d', [ABinding.PeerIP, APort]));
if tmpStr = SearchAim then
FUDP.Send(ABinding.PeerIP, ABinding.PeerPort, Format('%s:%d', [AimAnwser, LocalPort]));
end;
end;
function TUDPSearch.GetPort: Integer;
begin
Result := FUDP.DefaultPort;
end;
procedure TUDPSearch.SetPort(const Value: Integer);
begin
if FUDP.Active then
begin
raise Exception.Create('服务已开启TUDPSearch.SetPort调用失败');
Exit; //在运行时不能改
end;
if FUDP.DefaultPort <> Value then
begin
FUDP.DefaultPort := Value;
end;
end;
procedure TUDPSearch.SetUDPReadEvent(AUDPReadEvent: TUDPReadEvent);
begin
FUDP.OnUDPRead := AUDPReadEvent;
end;
function TUDPSearch.Stop: boolean;
begin
FUDP.Active := False;
Result := not FUDP.Active;
end;
展开全部
建立一个线程,在IdUDPClient连接上后不停调用IdUDPClient的ReceiveBuffer()方法 。
或改用其他控件,如ics。回答完毕,谢谢。。。。。。。
或改用其他控件,如ics。回答完毕,谢谢。。。。。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果没有让你满意的答案,就把200分给我吧!!!
我保佑你和家人健康 平安!!! !!!!~!!!!谢谢你啊!!!!!!
如果没有让你满意的答案,就把200分给我吧!!!
我保佑你和家人健康 平安!!! !!!!~!!!!谢谢你啊!!!!!!
我保佑你和家人健康 平安!!! !!!!~!!!!谢谢你啊!!!!!!
如果没有让你满意的答案,就把200分给我吧!!!
我保佑你和家人健康 平安!!! !!!!~!!!!谢谢你啊!!!!!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
去专业论坛看看,专业论坛的人都是很热心的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不好意思,帮不上你。我也不会这个
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询