RTC Forums
May 04, 2024, 11:42:20 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Broadcast from UDPServer  (Read 3986 times)
ClementDoss
RTC License
***
Posts: 36


« on: October 26, 2015, 09:09:18 PM »

Hi,

I need to notify my client applications that the Server is up and running.
When the server goes online (ie. starts to listen) I would like to broadcast a message to all the clients, using UDP.
Is there a way to make rtcUDPServer broadcast a message to any clients on the same network?
I assigned the following properties:

Code:
procedure TForm56.btnListenClick(Sender: TObject);
begin
  UDPServer.UdpMultiCastAddr := '192.168.0.255';
  UDPServer.UdpReuseAddr := True; // (Server and client might be on the same server)
  UDPServer.Listen;
end;

procedure TForm56.btnStopClick(Sender: TObject);
begin
   UDPServer.StopListen;
end;

procedure TForm56.btnBroadcastClick(Sender: TObject);
begin
  UDPServer.UdpMultiCast := True;
  UDPServer.Write('Hello guys');
  UDPServer.UdpMultiCast := False;
end;


Is it possible? Or should I use a rtcUDPClient on the server to broadcast and a rtcUDPServer on the clients to receive? Huh

Best regards,
Clément
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: October 26, 2015, 09:31:22 PM »

You should use TRtcUDPClient to broadcast messages and UDPServer to listen for packets and respond if necessary. TRtcUDPServer component doesn't broadcast, it only receives packets and can respond to the sender. Only TRtcUDPClient component can broadcast UDP packets or send packets to a specific recipient.

PS. In the Demos/GatewayTest folder, there is a RtcGateTestClient Project, which (if you declare the USE_UDP compiler directive) uses the TRtcUDPClient component to broadcast messages to all the clients on the same network and TRtcUDPServer component to receive and process such broadcasts, in case you need an example.

Best Regards,
Danijel Tkalcec
Logged
ClementDoss
RTC License
***
Posts: 36


« Reply #2 on: October 26, 2015, 11:30:11 PM »

Hi,

Great demo thanks!

I manage to make it work, but I need to get the broadcast message all the way to rtcHTTPClient.
For example, consider this scenario :
Client goes online - ( Server is not found )
Server goes online - ( Send broadcast message to all clients - Message contains HTTP server address and port )
Client configure itself ( Gets broadcast data and use it to setup HTTP properties )

Once the server goes online, I'm using a udpClient to broadcast the information I need.
The client is receiving the broadcast in UDPServer's  OnDataReceived event.
In the current context, the client received through UDPServer the address it needs to connect to the Server.
I received the data in rtcUDPServer thread. How can I pass that data to rtcHTTPClient safely?

Best regards,
Clément
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #3 on: October 27, 2015, 09:05:00 AM »

You just need to make sure that published properties on your TRtcHttpClient component are accessed from one thread at a time. Provided your TRtcHttpClient is created and configured from the Main Thread (which is usually the case), you can either use the TRtcUDPServer component in single-threaded mode (leave its MultiThreaded property at default state = False) or use the Sync() method to synchronize the TRtcUDPServer event with the Main Thread when you want to configure the TRtcHttpClient component using data received from your Server (remote TRtcUDPClient).

In short, just leave MultiThreaded=False on the TRtcUDPServer component Wink

Best Regards,
Danijel Tkalcec
Logged
ClementDoss
RTC License
***
Posts: 36


« Reply #4 on: October 27, 2015, 04:49:44 PM »

Thanks Danijel!

It's working perfectly!  Smiley

Clément
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.023 seconds with 18 queries.