How To Use UDP Multicasts.

This is few examples of using multicasts from SynaList. It is based on multicast demo from Microsoft MSDN.

Description: 234.5.6.7:22401 is one of class D IP address + port

procedure MulticastSendTest;
var
  sndsock:TUDPBlockSocket;
begin
  sndsock:=TUDPBlockSocket.Create;
  try
    sndsock.createsocket;
    sndsock.Bind('0.0.0.0','0');
    sndsock.MulticastTTL := 1;
    sndsock.connect('234.5.6.7','22401');
    sndsock.SendString('Ahoy!'+CRLF);
  finally
    sndsock.free;
  end;
end;
 
procedure MulticastRecvTest;
var
  rcvsock:TUDPBlockSocket;
  buf:string;
begin
  rcvsock:=TUDPBlockSocket.Create;
  try
    rcvsock.createsocket;
    rcvsock.Bind('0.0.0.0','22401');
    rcvsock.AddMulticast('234.5.6.7');
    buf:=rcvsock.RecvPacket(60000);
    showmessage(buf);
  finally
    rcvsock.free;
  end;
end;

…or this test in one procedure:

procedure multicasttest;
var
  sndsock:TUDPBlockSocket;
  rcvsock:TUDPBlockSocket;
  buf:string;
begin
  sndsock:=TUDPBlockSocket.Create;
  rcvsock:=TUDPBlockSocket.Create;
  try
    rcvsock.createsocket;
    rcvsock.Bind('0.0.0.0','22401');
    rcvsock.AddMulticast('234.5.6.7');
    Assert(rcvsock.LastError = 0);
 
    sndsock.createsocket;
    sndsock.Bind('0.0.0.0','0');
    sndsock.MulticastTTL := 1;
    sndsock.connect('234.5.6.7','22401');
    Assert(sndsock.LastError = 0);
 
    sndsock.SendString('Test Payload');
    buf:=rcvsock.RecvPacket(1000);
    showmessage(buf);
 
    sndsock.CloseSocket;
    rcvsock.CloseSocket;
  finally
    sndsock.free;
    rcvsock.free;
  end;
end;

:!: Note: The MultiCast-Support is in the Release (r37) broken because of this bug: http://www.mail-archive.com/synalist-public@lists.sourceforge.net/msg00731.html Latest release or Snapshots are OK!

public/howto/udpmulticast.txt · Last modified: 2008/01/05 16:34 by geby
Driven by DokuWiki Recent changes RSS feed