[erlang-questions] Sending UDP packets

Valentin Micic valentin@REDACTED
Fri Aug 21 18:08:45 CEST 2015


The case that you mentioned is quite curious, as UDP, by definition, may drop a message, but *must* honor the message boundaries.
In other words, UDP may *not* truncate the message -- messages are either delivered as a whole or not at all.

I am a big fan of UDP, however, I usually try to keep the UDP message size to the length lower that MTU (MTU - IP/UDP header).

Although possible, it is not advisable to send longer messages for a number of reasons, none of which, mind you, may cause message to be truncated, but rather dropped as a whole (assuming that OS kernel does its job).
Thus, if you have to send UDP message that is longer than MTU, you will be far better of writing a simple custom message disassembly-reassembly routines then relying on kernel's ability to transfer  big messages over UDP.

V/



On 21 Aug 2015, at 2:15 PM, Oscar Muñoz Garrigós 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




More information about the erlang-questions mailing list