Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Nov : Multiple bindings with TidUDPServer doesn't work?
| Subject: | Multiple bindings with TidUDPServer doesn't work? |
| Posted by: | "mike" (johnson..@zonnet.nl) |
| Date: | 8 Nov 2007 16:26:06 |
I am trying to start an indy udpserver which listens to multiple ports. I have tried to set multiple bindings designtime and runtime, but in both cases the onudpread event only seems to fire when all possible ports have been written to by the clients. What I am trying to achieve is that the onudpread fires whenever a client writes somthing to one of the listen ports. Can anybody please shed some light on this?
In the following code sample, I press the "write to port 1000" button and the onudpread fires as expected, but when I press it the second time nothing happens. Only if I press "write to port 1001" onudpread mysteriously fires twice, once for the 1001 port write and only then the "cached" 1000 port write suddenly emerges? Is this an Indy bug, or am I missing something?
[udpserversimple.pas]
unit udpserversimple;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
IdGlobal,Dialogs, IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer, IdUDPClient,
StdCtrls,IdSocketHandle;
type
TForm64 = class(TForm)
IdUDPServer1: TIdUDPServer;
IdUDPClient1: TIdUDPClient;
Button1: TButton;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TBytes;ABinding: TIdSocketHandle);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form64: TForm64;
implementation
{$R *.dfm}
procedure TForm64.Button1Click(Sender: TObject);
begin
IdUDPClient1.Send('localhost',1000,'TEST');
end;
procedure TForm64.Button2Click(Sender: TObject);
begin
IdUDPClient1.Send('localhost',1001,'TEST');
end;
procedure TForm64.FormCreate(Sender: TObject);
begin
IdUDPServer1.Bindings.add.port := 1000;
IdUDPServer1.Bindings.add.port := 1001;
IdUDPServer1.Active := TRUE;
end;
procedure TForm64.IdUDPServer1UDPRead(Sender: TObject; AData: TBytes;ABinding: TIdSocketHandle);
begin
showmessage('received on port: ' + inttostr(ABinding.port));
end;
end.
[udpserversimple.dfm]
object Form64: TForm64
Left = 0
Top = 0
Caption = 'Form64'
ClientHeight = 213
ClientWidth = 361
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 161
Top = 141
Width = 117
Height = 25
Caption = 'write to port 1000'
TabOrder = 0
OnClick = Button1Click
end
object Button2: TButton
Left = 161
Top = 180
Width = 117
Height = 25
Caption = 'write to port 1001'
TabOrder = 1
OnClick = Button2Click
end
object IdUDPServer1: TIdUDPServer
Bindings = <>
DefaultPort = 0
OnUDPRead = IdUDPServer1UDPRead
Left = 123
Top = 48
end
object IdUDPClient1: TIdUDPClient
Host = '0'
Port = 0
Left = 133
Top = 103
end
end