Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can TUdpSocket act both as a Client and a Server?

789 views
Skip to first unread message

dave

unread,
Aug 20, 2002, 9:01:02 PM8/20/02
to
Can TUdpSocket act both as a Client and a Server?

I am using Delphi6, learning the TUdpSocket.

1) I read the Delphi help about the component carefully.
I think that the component can act as Server and Client at the same
time.
Am I right?

2) So I wrote a small programm with a button to send UDP data with the
component
to my local computer,at the same time,I think ,TUdpSocket can listen
on the
local port, when data arrived OnReceive event will be triggered.
But this event never occured!

3) So I wrote a UDP server with Indy TIdUDPServer, It can receive the
data form
TUdpSocket,so the TUdpSocket can be a succeful UDP Client.

4) Now My question:
Does TUdpSocket realy can be used as an UDP server and
listens on local port automatically ?

Or anything wrong with my following code?

Or anybody can give me an example using the TUdpSocket as an UDP
server?

procedure TForm1.FormCreate(Sender: TObject);
begin
UdpSocket1.RemoteHost:='127.0.0.1';
UdpSocket1.RemotePort:='5678';
UdpSocket1.LocalPort:='5678';
UdpSocket1.Open;
end;

//send data ...
procedure TForm1.Button1Click(Sender: TObject);
var
Str0:string;
Len:Integer;
begin
Str0:=EditTextToSend.Text;
Len:=Length(Str0);
UdpSocket1.RemoteHost:='127.0.0.1';
UdpSocket1.RemotePort:='5678';
UdpSocket1.LocalPort:='5678';
UdpSocket1.Active:=True;
UdpSocket1.WaitForData;
UdpSocket1.SendBuf(Str0[1],Len);
end;

//receive data...
procedure TForm1.UdpSocket1Receive(Sender: TObject; Buf: PChar; var
DataLen: Integer);
begin
Memo1.Lines.Add('Data Received!');
end;

Thanks for your help!

Dave

Andy M.

unread,
Aug 21, 2002, 4:58:48 AM8/21/02
to
> 1) I read the Delphi help about the component carefully.
> I think that the component can act as Server and Client at the same
> time.
> Am I right?

Yes, however the component seems to have some problems, as others have
unsuccessfully tried to use it so far.

> 2) So I wrote a small programm with a button to send UDP data with the
> component
> to my local computer,at the same time,I think ,TUdpSocket can listen
> on the
> local port, when data arrived OnReceive event will be triggered.
> But this event never occured!

I don't have D6 currently installed, but I remember, that the standard mode
was blocking in D6. If that is the case, you need to set it to non-blocking
in order to receive events. Then you can leave out the WaitForData call as
well.

> Or anything wrong with my following code?

Yes, you are only using 1 socket!
This cannot work, you cannot send data from a socket to itself.
Place 2 TUDPSockets on your form and use 1 for sending and 1 for receiving!

Andy


dave

unread,
Aug 25, 2002, 10:22:41 PM8/25/02
to
Hi,Thank you for your answer.

I still can not make my poor UPD server working till now!
I just put one UdpSocket on the form,look at my code:


procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.Lines.Clear;
UdpSocket1.LocalPort:=EditLocalPort.Text;
UdpSocket1.BlockMode:=bmNonBlocking;
UdpSocket1.Open;
end;

procedure TForm1.UdpSocket1Receive(Sender: TObject; Buf: PChar;
var DataLen: Integer);
begin
Memo1.Lines.Add('Data Received!');
end;


The OnReceive event never happens even if I send UDP client data to it
through other applications which are sure to be correct !
Is there anything wrong with my code?

Thank you again!

Dave


Andy M.

unread,
Aug 26, 2002, 9:58:02 AM8/26/02
to
Well, I remember a few other posts with the same problem.
So far, the only conclusion for me is a bug in the component.
All others did the same as you do. There seems to be no mistake in the code.
Sadly, I cannot try it myself, as TUdpSocket was introduced in Delphi 6.

You could try blocking mode instead (and work without events) or use other
components (Indy, ICS, whatever...).
That's all I can advise you, but so far I have read no other advise from
anybody...

Andy


0 new messages