title: Erlang UDP packet not received
body: <p>I'm trying to get a client (behind a NAT) to send packet to a dedicated server.</p>
  
  <p>Here's my code : </p>
  
  <pre><code>-module(udp_test).
  
  -export([start_client/3, listen/1, send/4, start_listen/1]).
  
  start_client(Host, Port, Packet) -&gt;
      {ok, Socket} = gen_udp:open(0, [{active, true}, binary]),
      io:format("client opened socket=~p~n",[Socket]),
      spawn(?MODULE, send, [Socket, Host, Port, Packet]).
  
  start_listen(Port) -&gt;
      {ok, Socket} = gen_udp:open(Port, [binary]),
      spawn(?MODULE, listen, [Socket]).
  
  listen(Socket) -&gt;
      inet:setopts(Socket, [{active, once}]),
      receive
      {udp, Socket , Host, Port, Bin} -&gt;
          gen_udp:send(Socket, Host, Port, "Got Message"),
          io:format("server received:~p / ~p~n",[Socket, Bin]),
          listen(Socket)
      end.
  
  send(Socket, Host, Port, Packet) -&gt;
      timer:send_after(1000, tryToSend),
      receive
      tryToSend -&gt;
          io:fwrite("Sending: ~p / to ~p / P: ~p~n", [Packet, Host, Port]),
          Val = gen_udp:send(Socket, Host, Port, Packet),
          io:fwrite("Value: ~p~n", [Val]),
          send(Socket, Host, Port, Packet);
      _ -&gt;
          io:fwrite("???~n")
      end.
  </code></pre>
  
  <p>on the dedicated server I launch the listen function : </p>
  
  <pre><code># erl -pa ebin
  Erlang R15B01 (erts-5.9.1) [source] [64-bit] [smp:4:4] [async-threads:0] [kernel-poll:false]
  
  Eshell V5.9.1  (abort with ^G)
  1&gt; udp_test:listen(4000).
  </code></pre>
  
  <p>on the client side I launch the sending loop : </p>
  
  <pre><code>$ erl -pa ebin                                                      
  Erlang R15B (erts-5.9) [source] [smp:2:2] [async-threads:0] [hipe] [kernel-poll:false]
  
  Eshell V5.9  (abort with ^G)
  1&gt; udp_test:start_client("ip.of.my.server", 4000, "HELLO !!!").
  client opened socket=#Port&lt;0.737&gt;
  &lt;0.33.0&gt;
  Sending: "HELLO !!!" / to "ip.of.my.server" / P: 4000
  Value: ok
  Sending: "HELLO !!!" / to "ip.of.my.server" / P: 4000
  Value: ok
  Sending: "HELLO !!!" / to "ip.of.my.server" / P: 4000
  Value: ok
  </code></pre>
  
  <p>Although <strong>gen_udp:send</strong> from the client returns <strong>ok</strong>, the server doesn't seems to receive any of these packets, as it should print "<strong>server received: "HELLO !!!"</strong>" on the console.</p>
  
  <p>anyone would have an idea why this is not working.</p>
  
  <p><strong>EDIT 1 :</strong> </p>
  
  <ul>
  <li><p>There is no firewall or iptable configured on the dedicated server.</p></li>
  <li><p>The connection works fine through TCP between the client and the server, but not UDP.</p></li>
  <li><p>When I try to run both server and client on the same device (different erlang node), it does not work either.</p></li>
  </ul>
  
  <p><strong>EDIT 2 :</strong> 
  changed the code for the listen part looping on itself re-creating the Socket each time a message is received... but still does not work.</p>