[erlang-questions] Sending UDP packets

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Fri Aug 21 15:14:16 CEST 2015


This ought to work:

Including my typos:

Eshell V7.0.3  (abort with ^G)
1> {ok, S} = gen_udp:open(0, [inet]).
{ok,#Port<0.533>}
2> P = S.
#Port<0.533>
3> inet:port(P).
{ok,63315}
4> {ok, RP} = gen_udp:open(1234, [inet]).
{ok,#Port<0.549>}
5> flush().
ok
6> gen_udp:send(P, {127,0,0,1}, 1234, term_to_binary(hello)).
ok
7> flush().
Shell got {udp,#Port<0.549>,
               {127,0,0,1},
               63315,
               [131,100,0,5,104,101,108,108,111]}
ok
8> Bytes = [131,100,0,5,104,101,108,108,111].
[131,100,0,5,104,101,108,108,111]
9> binary_to_term(Bytes).
** exception error: bad argument
     in function  binary_to_term/1
        called as binary_to_term([131,100,0,5,104,101,108,108,111])
10> binary_to_term(iolist_to_binary(Bytes)).
hello

Immediate questions:

* You are not guaranteed message delivery when using UDP. What is your
strategy when this happens? Also, beware kernel settings! Usually the UDP
buffer at the kernel side is pretty small, so Erlang doesn't even get a
chance to run before the kernel throws away packets.

* What is your MTU? Over localhost, it can be pretty big:

OSX, FreeBSD:

tiefling:turtle jlouis$ ifconfig lo0 | head -n 1
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384

Linux can use a 64 kilobyte MTU on localhost.

But the Ethernet usually has an MTU of 1500 bytes or even lower, so you
cannot send an UDP message above that size without it truncating. What is
the iolist_size/1 of your Message?

You will need to split your message over several UDP datagrams then. And
you will have to handle that the internet may reorder your datagrams, or
lose them mid-flight.



On Fri, Aug 21, 2015 at 2:15 PM, Oscar Muñoz Garrigós <osmuogar@REDACTED>
wrote:

> Hi,
> I am using a server to send some packets. The problem is actually the
> packets are truncated and my client does not receive the full packet. I
> created two sockets but only the socket wich receives the packet returns it
> to the client, if i change it the packet is lost. Could you give me some
> advice?
>
> Here is the code:
>
>
>
> -behaviour(gen_server).
> init(_Args)->
>     ...
>     {ok,Socket} = gen_udp:open(49900),
>     {ok, #state{socket=Socket}}.
>
> handle_info(Info, State)->
>     error_logger:info_msg("Received via INFO: ~p~n", [Info]),
>     {udp, Socket, IP, InPortNo, Packet} = Info,
>     [{"from",From},{"to",To},{"text",Text}] = parser:read_xml(Packet),
>
>     write_to_db(Socket, IP, InPortNo, From, To, Text),
>     {noreply, State}.
>
> write_to_db(WSocket, WAdress, WMPort, WFrom, WTo, WText)->
>      ...
>      ...
>     Message = analizar_resultados(Respuesta),
>
>     {ok, SSocket} = gen_udp:open(0,[inet]),
>     %%ok = gen_udp:send(SSocket, WAdress, WMPort, Message), <-------- This
> does not works cause the packet is lost
>     ok = gen_udp:send(WSocket, WAdress, WMPort, Message), <-------- This
> works but the packet is truncated
>     gen_udp:close(SSocket),
>     ok.
>
>
>
>
>
> Thank you all for your help.
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>


-- 
J.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150821/eb1538e3/attachment.htm>


More information about the erlang-questions mailing list