Communication between applications using socket through UDP doing Multicast

5

I'm trying to make the communication between applications where one of them transmits a certain message and the others only receive, I do not know the IP address of the ones they will receive and they will not even be there to receive the message, multicast on the network.

I'm using Ararat Synapse with the TUDPBlockSocket class and found the following example on the component's 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 works fine if you open only 2 instances of the application and at first click the btnReceber button and the second click the btnEnviar button. However I open 3 instances, one sending and the other two receiving only one receives and the other stands there waiting for the 60000 milliseconds. The only two-instance test works only if both are running on the same machine, and I've already checked that my router was enabled with IGMP to allow multicast , but it still did not work over the network.

I'm using Delphi XE and did testing on Windows XP on a VM with Virtual Box and also on a Windows 7 computer.

    
asked by anonymous 05.03.2014 / 23:49

2 answers

2

Hello,

I do not know much about delphi but I already worked with multicast in C so I'll give my pitaco ...

Are you testing with 3 instances on the same machine? How did you make both client instances bind on the same port? The second should give some error in the bind (something of the type "address already in use") since normally you do not have 2 processes "listening" to a same port.

It would be useful to test the return of the bind () function to see if it really worked.

    
06.03.2014 / 03:02
2

Try using the Indy components like TIdUDPServer and TIdUDPClient

On receiving you treat like this:

procedure TMainHost.UDPServer2UDPRead(AThread: TIdUDPListenerThread;
  AData: TBytes; ABinding: TIdSocketHandle);
begin
//

//onde AData é o recebimento em bytes... 
//Você pode tratar os caracteres e montar sua mensagem com o Chr...

end;

In sending you treat like this:

function TMainHost.DoSendModificarCamera(
  aCamera: TCameraJSONRecognized): Integer;
var
  strTemp: String;
begin


  strTemp := 'string de envio';

  try

    IdUDPClient1.Host := '255.255.255.255';
    IdUDPClient1.Port := 34567;
    IdUDPServer2.Active := False;
    IdUDPServer2.DefaultPort := 34567;
    IdUDPClient1.Active := True;
    IdUDPClient1.Send(strTemp);
    IdUDPClient1.Active := False;
    IdUDPServer2.Active := True;
    Result := Length(x);
  except
    Result := -Length(x);
  end;

end;
    
07.03.2014 / 21:44