* * *

Author Topic: IDUDPSERVER how to send message to all subscribed clients ?  (Read 1255 times)

drama22

  • Newbie
  • Posts: 3
IDUDPSERVER how to send message to all subscribed clients ?
« on: December 17, 2017, 11:49:38 am »
i am trying to work with udpserver but things gone mess here is my server app code

Code: Pascal  [Select]
  1. unit UDPSRV;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdBaseComponent, IdComponent, IdUDPBase,
  8.   IdUDPServer, IdGlobal, IdSocketHandle, Vcl.StdCtrls;
  9.  
  10. type
  11.   Tudpservfrm = class(TForm)
  12.     udpserver: TIdUDPServer;
  13.     Button1: TButton;
  14.     Button2: TButton;
  15.     Edit1: TEdit;
  16.     Memo1: TMemo;
  17.     procedure udpserverUDPRead(AThread: TIdUDPListenerThread;
  18.       const AData: TIdBytes; ABinding: TIdSocketHandle);
  19.     procedure Button1Click(Sender: TObject);
  20.   private
  21.     { Private declarations }
  22.     procedure UpdateBindings;
  23.   public
  24.     { Public declarations }
  25.   end;
  26.  
  27. var
  28.   udpservfrm: Tudpservfrm;
  29.  
  30. implementation
  31.  
  32. {$R *.dfm}
  33.  
  34. procedure Tudpservfrm.Button1Click(Sender: TObject);
  35. begin
  36.  UpdateBindings;
  37.  
  38.  udpserver.Active := true;
  39. end;
  40.  
  41. procedure Tudpservfrm.udpserverUDPRead(AThread: TIdUDPListenerThread;
  42.   const AData: TIdBytes; ABinding: TIdSocketHandle);
  43. begin
  44. //
  45. //memo1.Lines.Add(bytestostring(AData));
  46.  
  47.  
  48. end;
  49.  
  50. procedure Tudpservfrm.UpdateBindings;
  51. var
  52. Binding: TIdSocketHandle;
  53. begin
  54. udpserver.DefaultPort := StrToInt(Edit1.Text);
  55. udpserver.Bindings.Clear;
  56. Binding := udpserver.Bindings.Add;
  57. Binding.IP := '0.0.0.0';
  58. Binding.Port := StrToInt(Edit1.Text);
  59. end;
  60.  
  61. end.

and here is my client code

Code: Pascal  [Select]
  1. unit udpfrm;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdBaseComponent, IdComponent, IdUDPBase,
  8.   IdUDPClient, Vcl.StdCtrls, Vcl.ExtCtrls, IdUDPServer, IdGlobal, IdSocketHandle;
  9.  
  10. type
  11.   TUDPClient = class(TForm)
  12.     Button1: TButton;
  13.     tmr1: TTimer;
  14.     Memo1: TMemo;
  15.     udpreciver: TIdUDPServer;
  16.     procedure Button1Click(Sender: TObject);
  17.     procedure tmr1Timer(Sender: TObject);
  18.     procedure udpreciverUDPRead(AThread: TIdUDPListenerThread;
  19.       const AData: TIdBytes; ABinding: TIdSocketHandle);
  20.     procedure udpreciverStatus(ASender: TObject; const AStatus: TIdStatus;
  21.       const AStatusText: string);
  22.   private
  23.     { Private declarations }
  24.   public
  25.     { Public declarations }
  26.   end;
  27.  
  28. var
  29.   UDPClient: TUDPClient;
  30.  
  31. implementation
  32.  
  33. {$R *.dfm}
  34.  
  35. procedure TUDPClient.Button1Click(Sender: TObject);
  36. begin
  37.   if not udpreciver.Active then
  38.   begin
  39.     udpreciver.Active := True;
  40.     udpreciver.Send('127.0.0.1',6754,'ping');
  41.     tmr1.Enabled := True;
  42.   end;
  43.  
  44. {udpclient.Host := '127.0.0.1';
  45. udpclient.Port := 6754;
  46. udpclient.Connect;}
  47. end;
  48.  
  49. procedure TUDPClient.tmr1Timer(Sender: TObject);
  50. begin
  51.   try if udpreciver.Active then
  52.  
  53.    udpreciver.Send('127.0.0.1',6754,'ping');
  54.  
  55.    except end;
  56. end;
  57.  
  58. procedure TUDPClient.udpreciverStatus(ASender: TObject;
  59.   const AStatus: TIdStatus; const AStatusText: string);
  60. begin
  61.  
  62. memo1.Lines.Add(AStatusText);
  63.  
  64. end;
  65.  
  66. procedure TUDPClient.udpreciverUDPRead(AThread: TIdUDPListenerThread;
  67.   const AData: TIdBytes; ABinding: TIdSocketHandle);
  68. begin
  69. memo1.Lines.Add(bytestostring(AData));
  70. end;
  71.  
  72. end.

i am trying to run this client code on 2 computer on the same network , but each client cannot recive what the other client sent its only received its own data , how can i share data between other clients that subscribed on the same port ?

Cyrax

  • Hero Member
  • *****
  • Posts: 579
Re: IDUDPSERVER how to send message to all subscribed clients ?
« Reply #1 on: December 17, 2017, 12:13:55 pm »
Are you using Delphi or Lazarus to develop your program?

Thaddy

  • Hero Member
  • *****
  • Posts: 6377
Re: IDUDPSERVER how to send message to all subscribed clients ?
« Reply #2 on: December 17, 2017, 01:09:23 pm »
Indeed that is Delphi code, not Freepascal/Lazarus.
I am enjoining wine, not whine....

drama22

  • Newbie
  • Posts: 3
Re: IDUDPSERVER how to send message to all subscribed clients ?
« Reply #3 on: December 17, 2017, 01:34:14 pm »
i am using delphi is it make a difference ?

Thaddy

  • Hero Member
  • *****
  • Posts: 6377
Re: IDUDPSERVER how to send message to all subscribed clients ?
« Reply #4 on: December 17, 2017, 01:56:40 pm »
i am using delphi is it make a difference ?

Yes. We do not support Delphi.
I am enjoining wine, not whine....

Remy Lebeau

  • Sr. Member
  • ****
  • Posts: 426
    • Lebeau Software
Re: IDUDPSERVER how to send message to all subscribed clients ?
« Reply #5 on: December 18, 2017, 07:03:31 pm »
i am trying to work with udpserver but things gone mess here is my server app code
...
i am trying to run this client code on 2 computer on the same network , but each client cannot recive what the other client sent its only received its own data , how can i share data between other clients that subscribed on the same port ?

This topic has been discussed recently on the Embarcadero forums:

https://forums.embarcadero.com/thread.jspa?threadID=271565

https://forums.embarcadero.com/thread.jspa?threadID=271565
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus