比如用NMUDP,你必须设置好它的LocalPort属性,使它和广播数据的端口一样。
procedure TForm1.NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer; FromIP: String; Port: Integer);
var
C: array [1..3] of Char;
I: Integer;
begin
if NumberBytes <= 3 then
begin
NMUDP1.ReadBuffer(C, I);
Memo1.Lines.Add(C+': received '+IntToStr(I)+' bytes from '+FromIP+' on port '+IntToStr(Port));
end
else
Memo1.Lines.Add(IntToStr(I)+' bytes incoming, buffer too small');
end;