two gen_udp questions

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

two gen_udp questions

Michael-22-3
Hi,

I have two question about udp handling:

1. can a gen_server also receive udp messages? When do I have to open the udp
port for doing this (start_link, init)?

2. if my server receives an udp message and spawns a process to handle the
request, is it better to let the spawned process sent the answer back over the
udp socket or should the process sent the answer back to my udp handler which
in turn sents it over the udp socket to it's destination.

thanks for answering,
 Michael




________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Reply | Threaded
Open this post in threaded view
|

Re: two gen_udp questions

Kaiduan Xie-2
Micha,

Yes, gen_server can receive udp message.

1. Open the socket in the init function of gen_server.
    init() ->
    ...
    gen_udp:open().

2. The incoming message will be handled in handle_info().

handle_info({udp, Socket, IPtuple, InPortNo, Packet}, State) ->

3. Either way will work.

Thanks,

kaiduan

On Sat, Aug 8, 2009 at 10:18 AM, Micha<[hidden email]> wrote:

> Hi,
>
> I have two question about udp handling:
>
> 1. can a gen_server also receive udp messages? When do I have to open the udp
> port for doing this (start_link, init)?
>
> 2. if my server receives an udp message and spawns a process to handle the
> request, is it better to let the spawned process sent the answer back over the
> udp socket or should the process sent the answer back to my udp handler which
> in turn sents it over the udp socket to it's destination.
>
> thanks for answering,
>  Michael
>
>
>
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Reply | Threaded
Open this post in threaded view
|

Re: two gen_udp questions

Michael-22-3
On Saturday, 8. August 2009 19:31:31 Kaiduan Xie wrote:
>
> 1. Open the socket in the init function of gen_server.

o.k.

> 2. The incoming message will be handled in handle_info().
>
> handle_info({udp, Socket, IPtuple, InPortNo, Packet}, State) ->

ah thanks, that info was good :-)

> 3. Either way will work.

o.k., can't there be a race condition, if multiple threads want to send data
over the socket?

thanks
 Michael


________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Reply | Threaded
Open this post in threaded view
|

Re: two gen_udp questions

Jim Morris-3
>
> o.k., can't there be a race condition, if multiple threads want to send data
> over the socket?

The UDP protocol does not guarantee packets are delivered in order, so
this should not be a concern, as the only side effect of several
processes sending data to the UDP socket would be them being out of
order.

However if you send all the UDP packets to a single process to send
them back and there are a lot of clients and/or packets you could
cause that process to get backed up, increasing the latency on the
packets and having the process message queue grow.
I have found it is better to have several processes send the reply
back to the same UDP socket, thereby distributing that load. The
downside is the processes have to be from the same VM so can't be
distributed over several servers (as the socket can only be shared by
the same VM).

Also note that when you do reply to a UDP packet to a client that is
behind a residential type NAT router/firewall the UDP packet generally
needs to come back from the same IP address and port to get through
the firewall/NAT device. Google STUN clients for more info.

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org