5
I’m trying to communicate between applications where one of them transmits a certain message and the others just receive, I don’t know the IP address of the ones that will receive and even if they won’t be there to receive the message, I just need to do a multicast on the net.
I’m using Ararat Synapse with the class TUDPBlockSocket
and found the following example on the component website...
Receiving the message
procedure TForm1.btnReceberClick(Sender: TObject);
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;
Sending the message
procedure TForm1.btnEnviarClick(Sender: TObject);
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;
This all works fine if opening only 2 instances of the application and at first click bo button btnReceber
and on the second click the button btnEnviar
. However, I open 3 instances, one sending and the other two receiving only one receives and the other is standing there waiting for the 60000 milliseconds. The test with only two instances works only if both are running on the same micro, I even checked if my router was enabled to allow the multicast, but it still didn’t work through the network.
I’m using Delphi XE and did tests on Windows XP on a VM with Virtual Box and also on a micro with Windows 7.
It’s been a long time (even) since I programmed in Delphi, so I don’t even risk an answer. But maybe the problem is in
bind
using0.0.0.0
. Have you tried using the external address of your network interface? This thread can help you understand what I mean: http://stackoverflow.com/questions/7556811/why-bind-a-socket-to-an-address– Luiz Vieira
Do you really need multicast? If it is in the same subnet, it would be enough to use simple UDP broadcast (address 192.168.0.255 on a 192.168.0.0/24 network, for example, or 10.255.255.255 on a 10.0.0.0/8 network)
– Bacco