Mega Search
23.2 Million


Sign Up

Make a donation  
UDP repeater?  
News Group: embarcadero.public.delphi.internet.winsock

I'm playing with UDP and I want to build a small app that acts as a repeater for SIP messages, something like this:

Originating client <----> UDP repeater <----> Destination server

A UDP server waits for messages from originating client, when it receives one, it send it using a UDP client to a destination server server. 
If the destination server sends messages back, they need to be passed to the originating client.

I was looking into IDMappedPortUDP, but that one works by sending one message to the destination and then waiting for a message back. 

In my situation the originating client sends one message and destination server could send multiple messages back and I have to pass them all to the originating client.

Can someone please point me in the right direction?

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 12-Jan-2015, at 9:44 AM EST
From: Avraam Prisca
 
Re: UDP repeater?  
News Group: embarcadero.public.delphi.internet.winsock
> {code}
> procedure TForm1.IdUDPServer1UDPRead(AThread: TIdUDPListenerThread; const 
> AData: TIdBytes; ABinding: TIdSocketHandle);
> var
>   DestIP: string;
>   DestPort: TIdPort;
> begin
>   if ABinding.PeerIP = ServerIP then
>   begin
>     DestIP := AppropriateClientIP;
>     DestPort := AppropriateClientPort;
>   end else begin
>     DestIP := ServerIP;
>     DestPort := ServerPort;
>   end;
>   ABinding.SendTo(DestIP, DestPort, AData);
> end;
> {code}
> 

That works perfect. Thanks.

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 13-Jan-2015, at 9:13 AM EST
From: Avraam Prisca
 
Re: UDP repeater?  
News Group: embarcadero.public.delphi.internet.winsock
Avraam wrote:

> I saw that this is exactly how IDMappedPortUDP works, but for each
> request waits only for one response. How would I make a udp client
> that connects to a server and sends a message, wait for multiple
> messages back?

The waiting logic is specific to the TIdMappedPortUDP component.  It does 
not apply to TIdUDPServer.  You would not have to do any waiting at all, 
just forward the packets as-is as you recieve them, eg:

{code}
procedure TForm1.IdUDPServer1UDPRead(AThread: TIdUDPListenerThread; const 
AData: TIdBytes; ABinding: TIdSocketHandle);
var
  DestIP: string;
  DestPort: TIdPort;
begin
  if ABinding.PeerIP = ServerIP then
  begin
    DestIP := AppropriateClientIP;
    DestPort := AppropriateClientPort;
  end else begin
    DestIP := ServerIP;
    DestPort := ServerPort;
  end;
  ABinding.SendTo(DestIP, DestPort, AData);
end;
{code}

--
Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 12-Jan-2015, at 2:12 PM EST
From: Remy Lebeau (TeamB)
 
Re: UDP repeater?  
News Group: embarcadero.public.delphi.internet.winsock
> Indy does not have a specialized component that perfectly suits your requirement. 
>  You will have to use the generic TIdUDPServer component and implement the 
> forwarding logic manually in its OnUDPRead event, where you will have access 
> to the PeerIP/PeerPort of the sender of each received packet.  When you receive 
> a packet from a client, send it to the destination server.  When you receive 
> a packet from a destination server, send it to the approproate client.  You 
> will have to keep track of that association yourself in your own code.

Hi Remy,

I saw that this is exactly how IDMappedPortUDP works, but for each request waits only for one response. How would I make a udp client that connects to a server and sends a message, wait for multiple messages back?

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 12-Jan-2015, at 10:22 AM EST
From: Avraam Prisca
 
Re: UDP repeater?  
News Group: embarcadero.public.delphi.internet.winsock
Avraam wrote:

> In my situation the originating client sends one message and destination
> server could send multiple messages back and I have to pass them all to
> the originating client.
> 
> Can someone please point me in the right direction?

Indy does not have a specialized component that perfectly suits your requirement. 
 You will have to use the generic TIdUDPServer component and implement the 
forwarding logic manually in its OnUDPRead event, where you will have access 
to the PeerIP/PeerPort of the sender of each received packet.  When you receive 
a packet from a client, send it to the destination server.  When you receive 
a packet from a destination server, send it to the approproate client.  You 
will have to keep track of that association yourself in your own code.

--
Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 12-Jan-2015, at 10:13 AM EST
From: Remy Lebeau (TeamB)